skip to Main Content

I have an array of objects containing information about city and state.
My array of objects look like this:

Sample Input

 "stateProvinces": [
{
  "cities": [
    {
      "code": "T001","citydescription": "Chennai"
    },
    {
      "code": "T002","citydescription": "Madurai"
    }
  ],
  "statecode": "TN"
},
{
  "cities": [
    {
      "code": "E001","citydescription": "Erakulam"
    },
    {
      "code": "M002","citydescription": "Munnar"
    }
  ],
  "stateCode": "KL"
}]

I need to filter based on statecode and citydescription. If I pass state code as TN and city description as Chennai
I need a following output

Sample output

Code: T001

Tried the following and able to filter based on state

const filtervalue = getCityNames.filter(code => {
            return code.statecode == "TN";
         });

2

Answers


  1. A straightforward way is to use 2 find()‘s.

    First find the matching province, then the matching city.

    Of course you can do this in a single command with eg reduce, but I think this is more readable:

    const data = {"stateProvinces": [{"cities": [{"code": "T001","citydescription": "Chennai"}, {"code": "T002","citydescription": "Madurai"} ], "statecode": "TN"}, {"cities": [{"code": "E001","citydescription": "Erakulam"}, {"code": "M002","citydescription": "Munnar"} ], "stateCode": "KL"}]}
    
    const findCode = 'TN';
    const findCity = 'Chennai';
    
    let res = null;
    
    const foundProvince = data.stateProvinces.find(o => o.statecode === findCode);
    if (foundProvince) {
        res = foundProvince.cities.find(c => c.citydescription === findCity)?.code;
    }
    
    console.log(res)
    Login or Signup to reply.
  2. From my two answering comments on another answer ..

    "for a vast amount of queries on a very large data base I would suggest transforming the data structure once into a map of maps … thus queries then would look like … stateCodeAndCityNameBasedCityCodeMap.get('TN')?.get?.('Chennai'); … or … stateCodeAndCityNameBasedCityCodeMap.get('KL')?.get?.('Munnar');"

    "Even though a find based iteration exits early the time complexity of a nested find is much higher then a straight linear access on a Map based query path for Map is optimized for fast lookups."

    … and in order to prove the above said …

    const stateProvinces = [{
      cities: [{
        code: "T001", citydescription: "Chennai",
      }, {
        code: "T002", citydescription: "Madurai",
      }],
      statecode: "TN",
    }, {
      cities: [{
        code: "E001", citydescription: "Erakulam",
      }, {
        code: "M002", citydescription: "Munnar",
      }],
      statecode: "KL",
    }];
    
    const stateCodeAndCityNameBasedCityCodeMap = new Map(
      stateProvinces.map(({ statecode, cities }) => [
        statecode,
        new Map(
          cities.map(({ citydescription, code }) => [ citydescription, code ])
        )
      ])
    );
    console.log(
      stateCodeAndCityNameBasedCityCodeMap.get('TN')?.get?.('Chennai')
    );
    console.log(
      stateCodeAndCityNameBasedCityCodeMap.get('KL')?.get?.('Munnar')
    );
    console.log(
      stateCodeAndCityNameBasedCityCodeMap.get('KL')?.get?.('FOO')
    );
    console.log(
      stateCodeAndCityNameBasedCityCodeMap.get('BAR')?.get?.('BAZ')
    );
    .as-console-wrapper { min-height: 100%!important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search