Return all matching elements of an array of objects?

You should invoke the Array.prototype.filter function there.

var filteredArray = YourArray.filter(function( obj ) {
    return obj.value === 1;
});

.filter() requires you to return the desired condition. It will create a new array, based on the filtered results. If you further want to operate on that filtered Array, you could invoke more methods, like in your instance .map()

var filteredArray = YourArray.filter(function( obj ) {
    return obj.value === 1;
}).map(function( obj ) {
    return obj.id;
});

console.log( filteredArrays ); // a list of ids

… and somewhere in the near future, we can eventually use the Arrow functions of ES6, which makes this code even more beauty:

var filteredArray = YourArray.filter( obj => obj.value === 1 ).map( obj => obj.id );

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)