How to Enumerate the Properties of a JavaScript Object

How do I enumerate the properties of a JavaScript object?

Simple enough:

for(var propertyName in myObject) {
// propertyName is what you want
// you can get the value like this: myObject[propertyName]
}

Now, you will not get private variables this way because they are not available.


EDIT: @bitwiseplatypus is correct that unless you use the hasOwnProperty() method, you will get properties that are inherited - however, I don't know why anyone familiar with object-oriented programming would expect anything less! Typically, someone that brings this up has been subjected to Douglas Crockford's warnings about this, which still confuse me a bit. Again, inheritance is a normal part of OO languages and is therefore part of JavaScript, notwithstanding it being prototypical.

Now, that said, hasOwnProperty() is useful for filtering, but we don't need to sound a warning as if there is something dangerous in getting inherited properties.

EDIT 2: @bitwiseplatypus brings up the situation that would occur should someone add properties/methods to your objects at a point in time later than when you originally wrote your objects (via its prototype) - while it is true that this might cause unexpected behavior, I personally don't see that as my problem entirely. Just a matter of opinion. Besides, what if I design things in such a way that I use prototypes during the construction of my objects and yet have code that iterates over the properties of the object and I want all inherited properties? I wouldn't use hasOwnProperty(). Then, let's say, someone adds new properties later. Is that my fault if things behave badly at that point? I don't think so. I think this is why jQuery, as an example, has specified ways of extending how it works (via jQuery.extend and jQuery.fn.extend).

How to list the properties of a JavaScript object?

In modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you can use the built in Object.keys method:

var keys = Object.keys(myObject);

The above has a full polyfill but a simplified version is:

var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}

Alternatively replace var getKeys with Object.prototype.keys to allow you to call .keys() on any object. Extending the prototype has some side effects and I wouldn't recommend doing it.

How to get all properties values of a JavaScript Object (without knowing the keys)?

By using a simple for..in loop:

for(var key in objects) {
var value = objects[key];
}

How do I loop through or enumerate a JavaScript object?

You can use the for-in loop as shown by others. However, you also have to make sure that the key you get is an actual property of an object, and doesn't come from the prototype.

Here is the snippet:

var p = {

"p1": "value1",

"p2": "value2",

"p3": "value3"

};

for (var key in p) {

if (p.hasOwnProperty(key)) {

console.log(key + " -> " + p[key]);

}

}

How to efficiently count the number of keys/properties of an object in JavaScript

To do this in any ES5-compatible environment, such as Node.js, Chrome, Internet Explorer 9+, Firefox 4+, or Safari 5+:

Object.keys(obj).length
  • Browser compatibility
  • Object.keys documentation (includes a method you can add to non-ES5 browsers)

Enumerate properties on an object

You have two options, using the Object.keys() and then forEach, or use for/in:

class stationGuide {
station1: any;
station2: any;
station3: any;

constructor(){
this.station1 = null;
this.station2 = null;
this.station3 = null;
}
}

let a = new stationGuide();
Object.keys(a).forEach(key => console.log(key));

for (let key in a) {
console.log(key);
}

(code in playground)



Related Topics



Leave a reply



Submit