简体   繁体   English

下划线以使用另一个数组过滤数组对象

[英]Underscore to filter array object using another array

How to get specific properties from array b. 如何从数组b获取特定属性。 Those properties to be filtered are in array a. 这些要过滤的属性在数组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"
}];
(function( property, x, y ) {
  var filters = _.pluck( x, property );
  var filter = function( obj ) {
    return _.pick( obj, filters );
  };
  return _.map( y, filter );
})( 'name', a, b );
var filters = _.pluck(a, 'name');

var c = _.map(b, function(el) {
    return _.pick(el, filters);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM