使用lodash过滤对象数组的对象

时间:2018-03-14 05:36:26

标签: javascript lodash

我有一个对象数组的对象,如下所示。我试图过滤出数组中数量大于0 的每个对象,并将其放入一个新变量中。因此,通过lodash,我尝试在_.filter中使用_.(myArr).forEach。但是这总是返回一个空数组。 _.filter中的console.log按原样显示quantity,但不会根据条件返回任何值。我在这里以正确的方式使用它还是有其他方法可以使用它来过滤掉它?

var data = [
  [{
    "aid": "1",
    "desc": "Desc 1",
    "name": "Name 1",
    "quantity": 1
  }, {
    "aid": "2",
    "desc": "Desc 2",
    "name": "Name 2",
    "quantity": 1
  }, {
    "aid": "3",
    "desc": "Desc 3",
    "name": "Name 3",
    "quantity": 0
  }],
  [{
    "aid": "4",
    "desc": "Desc 4",
    "name": "Name 4",
    "quantity": 0
  }, {
    "aid": "5",
    "desc": "Desc 5",
    "name": "Name 5",
    "quantity": 1
  }],
  [{
    "aid": "6",
    "desc": "Desc 6",
    "name": "Name 6",
    "quantity": 0
  }, {
    "aid": "7",
    "desc": "Desc 7",
    "name": "Name 7",
    "quantity": 0
  }]
];

var filtered;
_(data).forEach((d) => {
  filtered = _.filter(d, (o) => {
    return o.quantity > 0;
  });
});

console.log(filtered);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.0/lodash.min.js"></script>

3 个答案:

答案 0 :(得分:2)

您在每次迭代中覆盖filtered,因此其值仅代表forEach循环的最后一次迭代中发生的事情。

您应该将单个结果累积到数组中,例如使用push和扩展语法:

var filtered = [];

...并在循环中:

    filtered.push(..._.filter(d, (o) => o.quantity > 0);

请注意,您可以使用reducefilter等数组方法在vanilla JavaScript中执行此操作:

var data = [ [{ "aid": "1", "desc": "Desc 1", "name": "Name 1", "quantity": 1 }, { "aid": "2", "desc": "Desc 2", "name": "Name 2", "quantity": 1 }, { "aid": "3", "desc": "Desc 3", "name": "Name 3", "quantity": 0 }], [{ "aid": "4", "desc": "Desc 4", "name": "Name 4", "quantity": 0 }, { "aid": "5", "desc": "Desc 5", "name": "Name 5", "quantity": 1 }], [{ "aid": "6", "desc": "Desc 6", "name": "Name 6", "quantity": 0 }, { "aid": "7", "desc": "Desc 7", "name": "Name 7", "quantity": 0}]];

var filtered = data.reduce( 
    (filtered, d) => filtered.concat(d.filter( o => o.quantity > 0 )),
    []
);

console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

使用原生js:

let filtered = data.reduce((a, e) => a.concat(e)).filter(x => x.quantity > 0);

答案 2 :(得分:1)

或者您可以使用reduce

var output = data.reduce( 
    ( acc, c ) => acc.concat( c.filter( 
           s => s.quantity > 0 ) )  //filter out quantities > 0
    ,[]); //initialize accumulator to [] 

<强>演示

&#13;
&#13;
var data = [
  [{
    "aid": "1",
    "desc": "Desc 1",
    "name": "Name 1",
    "quantity": 1
  }, {
    "aid": "2",
    "desc": "Desc 2",
    "name": "Name 2",
    "quantity": 1
  }, {
    "aid": "3",
    "desc": "Desc 3",
    "name": "Name 3",
    "quantity": 0
  }],
  [{
    "aid": "4",
    "desc": "Desc 4",
    "name": "Name 4",
    "quantity": 0
  }, {
    "aid": "5",
    "desc": "Desc 5",
    "name": "Name 5",
    "quantity": 1
  }],
  [{
    "aid": "6",
    "desc": "Desc 6",
    "name": "Name 6",
    "quantity": 0
  }, {
    "aid": "7",
    "desc": "Desc 7",
    "name": "Name 7",
    "quantity": 0
  }]
];
var output = data.reduce( ( acc, c ) => acc.concat( c.filter( s => s.quantity > 0 ) )  ,[]); 
console.log(output);
&#13;
&#13;
&#13;