MarksCode
MarksCode

Reputation: 8604

Filter array of objects based on object's value

I have an array with several objects within. I want to retrieve the object with the property called types, which is an array, having an entry called "zoom". Here is what the array of objects looks like:

[Object, Object, Object]
0: Object
1: Object
2: Object
  exclude: "0"
  file: "/m/a/max_wind_zoom.jpg"
  position: "7"
  types: Array[1]
     0: "zoom"

So I want to only extract object 2 in this case since its property types has an entry zoom.

I'm really puzzled as how to achieve this.

Upvotes: 0

Views: 44

Answers (1)

Joe
Joe

Reputation: 82654

You can use Array.prototype.filter and Object.prototype.hasOwnProperty

[Object, Object, Object].filter(function (o) {
    return o.hasOwnProperty('types') && o.types.indexOf('zoom') > -1;
})

Upvotes: 2

Related Questions