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
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:
From my two answering comments on another answer ..
… and in order to prove the above said …