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.

3
  • Yes it's possible, what have you tried so far? Commented Jan 6, 2012 at 2:14
  • 1
    How do you define that an object is a door? Is it an instance of a javascript class? How have you defined this class? Commented Jan 6, 2012 at 2:15
  • Here is another example question: - stackoverflow.com/questions/208016/… Commented Jan 6, 2012 at 2:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Worth noting IE8 makes me sad :(
Hah, but just be happy we can stop noting that nothing works in IE6.
@JamesMontagne which part of the solution is a problem for IE8?
@JaredPar Object.keys, Array#reduce

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.