Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enumerate the properties of a JavaScript object? [duplicate]

How do I enumerate the properties of a JavaScript object?

I actually want to list all the defined variables and their values, but I've learned that defining a variable actually creates a property of the window object.

like image 384
davenpcj Avatar asked Sep 17 '08 18:09

davenpcj


People also ask

How can I check if the array of objects have duplicate property values?

To check if the array of objects have duplicate property values with JavaScript, we can use the JavaScript array's map and some method to map the array to the property values we want to check.


2 Answers

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).

like image 136
Jason Bunting Avatar answered Sep 28 '22 19:09

Jason Bunting


Use a for..in loop to enumerate an object's properties, but be careful. The enumeration will return properties not just of the object being enumerated, but also from the prototypes of any parent objects.

var myObject = {foo: 'bar'};  for (var name in myObject) {   alert(name); }  // results in a single alert of 'foo'  Object.prototype.baz = 'quux';  for (var name in myObject) {   alert(name); }  // results in two alerts, one for 'foo' and one for 'baz' 

To avoid including inherited properties in your enumeration, check hasOwnProperty():

for (var name in myObject) {   if (myObject.hasOwnProperty(name)) {     alert(name);   } } 

Edit: I disagree with JasonBunting's statement that we don't need to worry about enumerating inherited properties. There is danger in enumerating over inherited properties that you aren't expecting, because it can change the behavior of your code.

It doesn't matter whether this problem exists in other languages; the fact is it exists, and JavaScript is particularly vulnerable since modifications to an object's prototype affects child objects even if the modification takes place after instantiation.

This is why JavaScript provides hasOwnProperty(), and this is why you should use it in order to ensure that third party code (or any other code that might modify a prototype) doesn't break yours. Apart from adding a few extra bytes of code, there is no downside to using hasOwnProperty().

like image 38
Ryan Grove Avatar answered Sep 28 '22 18:09

Ryan Grove