简体   繁体   English

如何过滤/搜索多个子数组?

[英]How can I filter/search multiple subarrays?

I am working on a search function for a parent child structure with locations (city,suburb,street).我正在研究具有位置(城市、郊区、街道)的父子结构的搜索功能。 I want to search for a street name and get the results but keeping the structure.我想搜索街道名称并获得结果但保留结构。

The array structure is like this:数组结构是这样的:

const cities = [
  {
    name: 'city1', sublocations: [
      {
        name: 'suburb1', sublocations: [
          {name: 'street1'},
          {name: 'street2'},
          {name: 'street3'},
        ]
      }, {
        name: 'suburb2', sublocations: [
          {name: 'street1'},
          {name: 'street2'},
          {name: 'street3'},
        ]
      }
    ]
  }
];

When I search for street1 for example I want to get this:例如,当我搜索street1 时,我想得到这个:

const cities = [
  {
    name: 'city1', sublocations: [
      {
        name: 'suburb1', sublocations: [
          {name: 'street1'},
        ]
      }, {
        name: 'suburb2', sublocations: [
          {name: 'street1'},
        ]
      }
    ]
  }
];

I tried it with the array.filter function but it overwrites the data and when I remove the search string the rest of the array doesn't come back.我用 array.filter 函数尝试了它,但它覆盖了数据,当我删除搜索字符串时,数组的其余部分不会回来。

I think this snippet should do the trick :我认为这个片段应该可以解决问题:

 var cities = [ {name :'city1', sublocations:[ {name :'suburb1', sublocations:[ {name :'street1'}, {name :'street2'}, {name :'street3'}, ]}, {name :'suburb2', sublocations:[ {name :'street1'}, {name :'street2'}, {name :'street3'}, ]} ]} ]; let findStreet = function(streetName) { return cities.map(city=> ({...city, sublocations:city.sublocations.map(suburb=> ({...suburb, sublocations:suburb.sublocations.filter( street=>street.name===streetName ) }) )}) ) } console.log(findStreet("street1"));

If you need a bit more flexibility you could generalize the location type by adding a type key, eg:如果您需要更大的灵活性,您可以通过添加type键来概括位置类型,例如:

{ type: 'city', name: 'city1', locations: [] }

And then use a recursive filter function to make a decision for each node in the tree, eg:然后使用递归过滤器函数为树中的每个节点做出决定,例如:

const filterTree = (nodes, childrenKey, selector) => {
  return nodes.filter(selector).map(node => {
    return node.hasOwnProperty(childrenKey)
      ? {...node, [childrenKey]: filterTree(node[childrenKey], childrenKey, selector)}
      : node;
  });
};

Example:例子:

 const locations = [ { type: 'city', name: 'city1', locations: [ { type: 'suburb', name: 'suburb1', locations: [ { type: 'street', name: 'street1' }, { type: 'street', name: 'street2' }, { type: 'street', name: 'street3' }, ] }, { type: 'suburb', name: 'suburb2', locations: [ { type: 'street', name: 'street1' }, { type: 'street', name: 'street2' }, { type: 'street', name: 'street3' }, ] } ] } ]; const filterTree = (nodes, childrenKey, selector) => { return nodes.filter(selector).map(node => { return node.hasOwnProperty(childrenKey) ? {...node, [childrenKey]: filterTree(node[childrenKey], childrenKey, selector)} : node; }); }; // example const filtered = filterTree(locations, 'locations', ({type, name}) => { return type === 'city' // allow all cities || (type === 'suburb' && name === 'suburb2') // but only specific suburbs || (type === 'street' && name === 'street2') // and streets; }); console.log(filtered);

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

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