如何过滤多维数组?

时间:2019-06-25 09:21:38

标签: ionic-framework ionic2

我有一个数组。我想根据他们的部门过滤数据。

我尝试了以下代码

this.arr.filter((item)=> {
             return (item.NAME.indexOf(value) > -1);
           });

this.arr = [{DEPT : "A"
DETAIL: {NAME:"ABC", CODE:"1"}
}
{DEPT : "B"
DETAIL: {NAME:"TEST",CODE:"2"}
}]

我希望输出

[{DEPT : "A"
DETAIL: {NAME:"ABC", CODE:"1"}
}]

但实际输出是

[{DEPT : "A"
DETAIL: {NAME:"ABC", CODE:"1"}
}
{DEPT : "B"
DETAIL: {NAME:"TEST",CODE:"2"}
}]

1 个答案:

答案 0 :(得分:0)

此代码符合您的期望:

arr = [{
    DEPT: "A",
    DETAIL: { NAME: "ABC", CODE: "1" }
  },
  {
    DEPT: "B",
    DETAIL: { NAME: "TEST", CODE: "2" }
  }];

此代码

console.log(this.arr.filter((item) => item.DETAIL.NAME.indexOf('ABC') > -1 ))

打印:

[{
    DEPT: "B",
    DETAIL: { NAME: "TEST", CODE: "2" }
}]

工作stackblitz