通过数组元素数组进行数组过滤?

时间:2020-01-09 18:15:17

标签: javascript arrays

我有示例数据

[{
    "section": "RADIOGRAPHY",
    "sub_type": "ABDOMEN & PELVIS",
    "list": [{
        "item": "Abdomen",
      },
      {
        "item": "Abdomen/Pelvis"
      }
    ]
  },
  {
    "section": "SCANNING",
    "sub_type": "ABDOMEN & PELVIS",
    "list": [{
        "item": "first",
      },
      {
        "item": "second"
      }
    ]
  }
]

我想过滤深层数组,就像我按list item name搜索一样,它应该可能匹配

如果我使用fir搜索,它应该将匹配的对象返回到该条件,但不会唤醒

 let temp = this.name.map(function(ele){
    return ele.list.filter(el => {
      return el['item'].includes(`${value}`)
    })
  });
  console.log(temp);

4 个答案:

答案 0 :(得分:1)

您可以使用过滤器而不是地图,在第二个查找中使用find

const temp = name.filter(function(ele){
    return ele.list.find(el => {
      return el['item'].includes(`fir`)
    })
  });

答案 1 :(得分:0)

你看起来像这样吗?

let arr = [
    {
    "section": "RADIOGRAPHY",
    "sub_type": "ABDOMEN & PELVIS",
    "list": [{
      "item": "Abdomen",
    },
    {
      "item": "Abdomen/Pelvis"
    }
    ]},
    {
    "section": "SCANNING",
    "sub_type": "ABDOMEN & PELVIS",
    "list": [{
      "item": "first",
    },
    {
      "item": "second"
    }
    ]}
]
let toSearch = 'Abd'; 

let array = arr.map(a=>a.list).flat().filter(o => o.item.includes(toSearch));

console.log(array);

答案 2 :(得分:0)

您可以尝试一下。

let tmp = this.name.map( ele => ele.list.filter(el => el.item.includes(`${value}`)))

答案 3 :(得分:0)

这将为您工作。试试看。

let input = "fir";
let temp = [];
arr.forEach(item => item.list.forEach( e => e.item.search(input.trim()) > -1 ? temp.push(e) : ""));

console.log(temp);

此输出将是:

[ { item: 'first' } ]