Getting properties of an object

I hope this will be simple, but I can’t seem to find the answer. How do I get a certain property of all child objects within an object. For example: So my object has many children, all named differently, but I want the id property from all of them and push them onto a new array.

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

Yes, that is exactly what I was looking for.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.