简体   繁体   English

数组内的对象内的对象内的数组 JavaScript - 搜索

[英]Object inside array inside object inside array JavaScript - Search

I have a data like this:我有这样的数据:

 const bigData = [ { id: '1', data: [ {category: 'swim', value: 'Abc'}, {category: 'ran', value: '123'}, {category: 'play', value: 'Test'} ] }, { id: '2', data: [ {category: 'swim', value: 'Abc'}, {category: 'ran', value: ''}, {category: 'play', value: 'abc'} ] }, { id: '3', data: [ {category: 'swim', value: 'String that I Need'}, {category: 'ran', value: '456'}, {category: 'play', value: 'Testtest'} ] } ]

I want to get array of objects if one of this objects contains part of the string in their value.如果此对象之一在其值中包含部分字符串,我想获取对象数组。

For example, if the search string is "String tha" it should return例如,如果搜索字符串是“String tha”,它应该返回

   [ {
    id: '3',
    data: [
      {category: 'swim', value: 'String that I Need'},
      {category: 'ran', value: '456'},
      {category: 'play', value: 'Testtest'}
    ]
  }]

And if the search string is "Abc" it should return如果搜索字符串是“Abc”,它应该返回

[{
        id: '1',
        data: [
          {category: 'swim', value: 'Abc'},
          {category: 'ran', value: '123'},
          {category: 'play', value: 'Test'}
        ]
      },
      {
        id: '2',
        data: [
          {category: 'swim', value: 'somethinf'},
          {category: 'ran', value: ''},
          {category: 'play', value: 'Abcd'}
        ]
      },]

This is what I have so far, not sure if it is even the right direction to go:到目前为止,这就是我所拥有的,不确定这是否是正确的方向:

const arr = bigData.map(
      (item) => item.data
    )
    const res = arr
      ?.map((item) => item.map((e) => e.value))
      .filter((i) => i.map((e) => e.includes(search) === true))

There's no reason to map() the outer array to each object's data member, since the return value and the input value have the same layout.没有理由将外部数组map()到每个对象的data成员,因为返回值和输入值具有相同的布局。 Instead, you're performing a filter() on the outer array bigData , which should keep objects whose inner array member data satisfies a certain condition.相反,您在外部数组bigData上执行filter() ,它应该保留内部数组成员data满足特定条件的对象。

You can check for that condition using some() , since it is applied to each element of the array, and you only care whether or not there exists an element that matches.您可以使用some()检查该条件,因为它应用于数组的每个元素,并且您只关心是否存在匹配的元素。

 function filterBySearch (search) { return bigData.filter( ({ data }) => data.some( ({ value }) => value.includes(search) ) ); } const bigData = [ { id: '1', data: [ {category: 'swim', value: 'Abc'}, {category: 'ran', value: '123'}, {category: 'play', value: 'Test'} ] }, { id: '2', data: [ {category: 'swim', value: 'Abc'}, {category: 'ran', value: ''}, {category: 'play', value: 'abc'} ] }, { id: '3', data: [ {category: 'swim', value: 'String that I Need'}, {category: 'ran', value: '456'}, {category: 'play', value: 'Testtest'} ] } ]; console.log(filterBySearch('String tha')); console.log(filterBySearch('Abc'));
 .as-console-wrapper{max-height:100%!important;}

The easiest approach is the one liner already provided by Patrick.最简单的方法是 Patrick 已经提供的一种衬垫。 You are on the right path, but just getting tripped up on the proper use of the array functions.您走在正确的道路上,但只是对正确使用数组函数感到困惑。 If you wanted to write this as a function, it would look like:如果你想把它写成一个函数,它看起来像:

function searchBigData(bigData, searchText) {
    return bigData.filter(entry => entry.data.some(item => item.value.includes(searchText)));
}

For future reference, the basic strategy here is:为了将来参考,这里的基本策略是:

  1. Use filter on the bigData array because you want to reduce that down to the original objects that meet your criteria.对 bigData 数组使用filter ,因为您希望将其减少到符合条件的原始对象。
  2. For each item in bigData, use some against the data array.对于 bigData 中的每个项目,对data数组使用some It will return true as soon as one of the items in data matches the condition.只要data中的一项与条件匹配,它就会返回 true。
  3. Test each item in data for the value to include the search text测试data中的每个项目的value以包含搜索文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM