, jQuery, , map filter .
However, if you combine jQuery with the popular Underscore.js library, the problem becomes trivial thanks to the compact Underscore method:
var myMap = arr.map(function(e) {
return e.id;
});
myMap = _(myMap).compact();
But if you use Underscore, there is an even easier way to do the same, because Underscore has a method specifically for extracting a specific property from an object:
var myMap = _(arr).pluck('id');
myMap = _(myMap).compact();
You can even connect the two together:
var myMap = _(arr).chain().pluck('id').compact().value();
or you can invest in the Greids Underscore library to get what I consider the easiest solution:
var myMap = _(arr).pluck_('id').compact();
source
share