Getting properties of an object

You can iterate over the properties of an object using the for…in construct, but this also includes properties from other objects along the prototype chain (i.e. objects that the current object inherits from) so you have to use the hasOwnProperty method to check.

// assuming 'parent' is the object you want to iterate over
var childIds = [];
for (var child in parent) {
  if (parent.hasOwnProperty(child)) {
    childIds.push(parent[child].id);
  }
}
4 Likes