Reputation: 1151
How to get specific properties from array b. Those properties to be filtered are in array a.
Is there any easier way to do using underscore.
var a = [{
name: "code"
}, {
name: "barcode"
}, {
name: "status",
type: "button"
}];
var b = [{
id: 1,
code: 10,
barcode: "121212",
status: "success",
amount: "10",
available: true
}, {
id: 1,
code: 10,
barcode: "121212",
status: "success",
amount: "10",
available: true
}];
Now if using underscore how can I get below result
var c = [{
code: 10,
barcode: "121212",
status: "success"
}, {
code: 10,
barcode: "121212",
status: "success"
}];
Upvotes: 0
Views: 378
Reputation: 4366
(function( property, x, y ) {
var filters = _.pluck( x, property );
var filter = function( obj ) {
return _.pick( obj, filters );
};
return _.map( y, filter );
})( 'name', a, b );
Upvotes: 1
Reputation: 1795
var filters = _.pluck(a, 'name');
var c = _.map(b, function(el) {
return _.pick(el, filters);
});
Upvotes: 1