MarksCode MarksCode - 9 months ago 32
Javascript Question

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

, 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
has an entry

I'm really puzzled as how to achieve this, can anyone please help me out?

Joe Joe

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;