0

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"
}];

2 Answers 2

1
var filters = _.pluck(a, 'name');

var c = _.map(b, function(el) {
    return _.pick(el, filters);
});
Sign up to request clarification or add additional context in comments.

Comments

1
(function( property, x, y ) {
  var filters = _.pluck( x, property );
  var filter = function( obj ) {
    return _.pick( obj, filters );
  };
  return _.map( y, filter );
})( 'name', a, b );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.