1

I have an object which has numerous properties in it. Now I want to access certain properties. For example, object is Car. Car has further properties which are actually objects, for example doors, wheels. Now there can be three, four or five or may be more doors in a car.

Now I want to get a list of object which are doors. Is it possible.

huber.duber
  • 762
  • 2
  • 7
  • 31

1 Answers1

1
var doors = Object.keys(car).reduce(function (doors, name) {
  var potentialDoor = car[name];
  if (Door.isPrototypeOf(potentialDoor)) {
    doors.push(potentialDoor);
  }
  return doors;
}, []);

This makes a broad assumption that a car contains properties which are of "class" Door. And then returns an array of those properties.

Raynos
  • 166,823
  • 56
  • 351
  • 396