我对ID / nume的数组(对象)有异议:

Object { Biosecuritate=[Object {id=1,nume="bio"},
Object {id=5,nume="securitate"}], Deseuri=[Object {id=2,nume="des"}] ,
Reglementari=[Object {id=3,nume="Reglementari"}],
Sol/Subsol=[Object {id=4,nume="Sol/Subsol"}] }

    var filtered = _.filter(domenii, function(item) {
            return (item[0].nume == "Reglementari" || item[0].nume == "Sol/Subsol")
    });
    console.log("filtered=",filtered);
    console.log("domenii=",domenii);


我试图从“矩阵”中仅“提取”两个对象,但是在执行过滤器之后
它成为了:

[ [Object {id=3,nume="Reglementari"}], [Object {id=4,nume="Sol/Subsol"}] ]


如何将“已过滤的”转换为初始类型,或者在不更改的情况下将其过滤为“形式”?

最佳答案

要维护对象,可以减少而不是过滤:

var domenii = {
  Biosecuritate: [{id:1,nume:"bio"}, {id:5,nume:"securitate"}],
  Deseuri: [{id:2,nume:"des"}] ,
  Reglementari: [{id:3,nume:"Reglementari"}],
  "Sol/Subsol": [{id:4,nume:"Sol/Subsol"}]
}

var reduced = _.reduce(domenii, function(memo, item, key){
  if(item[0].nume == "Reglementari" || item[0].nume == "Sol/Subsol"){
    memo[key] = item;
  }
  return memo
}, {})

console.log("reduced=",reduced);
console.log("domenii=",domenii);

10-05 19:49