skip to Main Content

I am trying to solve a difficult computation but i am not reaching anywhere. can someone please help

Here is a simple example.

const filterCityBy = "NY";
const data = [ 
  { name: "Harry, age: 45, city: "NY" },
  { name: "Mike, age: 36, city: "CA" }
]

const filteredData = data.filter(x = x.city === filterCityBy);

output = [ { name: "Harry, age: 45 } ]

This is self explainatory. I am trying to filter data with value filterCityBy and getting the desired output

But my requirement is as follows

const filterCityBy = "NY";
const data = [ 
  { name: "Harry, age: 45, totalCities: [{ city: "NY"}]},
  { name: "Mike, age: 36, totalCities: [{ city: "NY"}, {city: "CA"} }] }
]

output = [ { name: "Harry, age: 45 }, { name: "Mike, age: 36 } ]

How can i achieve the desired output. Harry and Mike both has city as NY in their totalCities array. I only need their name and age once the city is present in their totalCities array.

Can someone please help on how to get this.

3

Answers


  1. Combine filter() with some() to check if some of the totalCities matches filterCityBy.

    Then use map() to get only the name and age key by removing totalCities using a destructuring assignment (...r):

    const filterCityBy = "NY";
    const data = [ 
      { name: "Harry", age: 45, totalCities: [{ city: "NY"}]},
      { name: "Mike", age: 36, totalCities: [{ city: "NY"}, {city: "CA"} ] }
    ]
    
    const output = data
        .filter(p => p?.totalCities.some(({ city }) => city === filterCityBy))
        .map(({ totalCities, ...r }) => r);
                       
    console.log(output);
    [
      {
        "name": "Harry",
        "age": 45
      },
      {
        "name": "Mike",
        "age": 36
      }
    ]
    
    Login or Signup to reply.
  2. You could filter the array and map the result without totalCities.

    const
        filterCityBy = "NY";
        data = [{ name: "Harry", age: 45, totalCities: [{ city: "NY" }] }, { name: "Mike", age: 36, totalCities: [{ city: "NY" }, { city: "CA" }] }],
        result = data
            .filter(({ totalCities }) => totalCities.some(({ city }) => city === filterCityBy))
            .map(({ totalCities, ...o}) => o);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  3. Check your example code and result. The filter method alone would not give that result once you fix the code. You would need the map method as well, as pointed out by others already. Here is another way you can do it:

    const 
        filterCityBy = "NY",
        data = [ 
          { name: "Harry", age: 45, totalCities: [{ city: "NY"}]},
          { name: "Mike", age: 36, totalCities: [{ city: "NY"}, {city: "CA"}] }
        ],
        
        filteredData = data.filter(
            ({totalCities}) => totalCities.some(({city}) => city === filterCityBy)
        )
        .map(({name,age}) => ({name,age}));
        
    console.log( filteredData );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search