0

我有样本数据

[{
    "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

4 回答 4

1

可以用filter代替map,在第二个find uset find

const temp = name.filter(function(ele){
    return ele.list.find(el => {
      return el['item'].includes(`fir`)
    })
  });
于 2020-01-09T18:22:27.217 回答
0

你可以试试这个。

let tmp = this.name.map( ele => ele.list.filter(el => el.item.includes(`${value}`)))
于 2020-01-09T18:30:13.463 回答
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' } ]
于 2020-01-09T18:59:25.327 回答
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);

于 2020-01-09T18:26:30.557 回答