JavaScript How can I filter/search multiple subarrays? filter search multiple values excel,filter search multiple values,filter

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:

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.

Answer:1

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"));
Answer:2

It's silly, I know - every time I change environment I comment out and back my app's config factory. There must be a better way, is there? I though of checking the URL but this seems like a bad ...

It's silly, I know - every time I change environment I comment out and back my app's config factory. There must be a better way, is there? I though of checking the URL but this seems like a bad ...

When would I do <CustomComponent function = {this.FunctionName}/> instead of <CustomComponent function = {this.FunctionName.bind(this)}? React's documentation mentions that bind() solves ...

When would I do <CustomComponent function = {this.FunctionName}/> instead of <CustomComponent function = {this.FunctionName.bind(this)}? React's documentation mentions that bind() solves ...

  1. please simply explain

I have a small issue with a recursive .find() . when I use it to find ID === 2, it returns be the object with ID === 1 ... I have been scratching my head and I don't understand why that happens my ...

I have a small issue with a recursive .find() . when I use it to find ID === 2, it returns be the object with ID === 1 ... I have been scratching my head and I don't understand why that happens my ...

I'm learning JS/Jquery and I'm trying to make a part of a form show and hide when clicking on part of the form. I am able to open part of the form, but not to close it again. As there is no error ...

I'm learning JS/Jquery and I'm trying to make a part of a form show and hide when clicking on part of the form. I am able to open part of the form, but not to close it again. As there is no error ...

  1. show hide part of text jquery
  2. css-only hide/show part of table
  3. show hide part of div