How to work with objects in JavaScript?

How to work with an object if I want ...

var object = { 'title': value };

alert( object[ /* Whatever */ ] ); // Should return 'title' NOT value

Thank.

+3
source share
1 answer

Use a loop for...into list the keys of an object, for example:

for(var key in object) {
  alert(key);  //to get the key value, use object[key]
}

To be safe, if someone messes up the prototype of an object, use .hasOwnProperty()as follows:

for(var key in object) {
  if(object.hasOwnProperty(key)) {
    alert(key);
  }
}
+7
source

Source: https://habr.com/ru/post/1777051/


All Articles