I need to match values from two JSON sources. Using the javascript find method this way works for me when the nesting of the "cities" array is one level more shallow (just an array of objects), but it's not working with deeper nesting (an array of objects within an array of objects).
Essentially, what I am trying to do is loop through the feeds[0].feed.details.place array and find the matching cities.CountyPlaces.PlaceFIPSCode value for each. I actually need the entire "place" object, so I can utilize any data within for each match.
// console.log(feeds[0].feed.details.place);
// console.log(cities[1].CountyPlaces[2].PlaceName);
feeds[0].feed.details.place.map(async (arrItem, z) => {
// console.log('arrItem: ', arrItem);
const cityMatch = await cities.find((cityObject, i) => {
// console.log(i, 'cityObject: ', cityObject);
arrItem === cityObject.PlaceName;
});
if (cityMatch !== undefined) {
// --> THIS IS WHERE I NEED TO MANIPULATE MATCHING DATA
console.log(
z,
'cityMatch: ',
arrItem,
cityMatch.PlaceName,
cityMatch.PlaceFIPSCode
);
} else {
// there should be a defined match for every "place" and no else results
console.log(z, '💥 cityMatch UNDEFINED', arrItem);
}
});
Here is a simplified example of the data I am using, with identical nesting:
const feeds = [
{
feed: {
record: '0002',
details: {
county: ['Alameda'],
place: ['Alameda', 'Berkeley', 'Oakland'],
},
},
},
];
const cities = [
{
CountyName: 'San Francisco',
CountyFIPSCode: '075',
CountyPlaces: [
{
PlaceName: 'San Francisco',
PlaceFIPSCode: '67000',
},
],
},
{
CountyName: 'Alameda',
CountyFIPSCode: '001',
CountyPlaces: [
{
PlaceName: 'Alameda',
PlaceFIPSCode: '00562',
},
{
PlaceName: 'Albany',
PlaceFIPSCode: '00674',
},
{
PlaceName: 'Berkeley',
PlaceFIPSCode: '06000',
},
{
PlaceName: 'Emeryville',
PlaceFIPSCode: '22594',
},
{
PlaceName: 'Oakland',
PlaceFIPSCode: '53000',
},
],
},
];
find()isn't async so no need to await. But.PlaceNameexists on objects inside thecityObject.CountyPlacesarray, so you'll need to iterate that array, probably withsome()details.county, which it seems you should given that different counties may have places with the same name