I am facing an issue with matching values from two JSON sources. While using the javascript find
method, I have found that it works when the nesting of the "cities" array is one level more shallow (just an array of objects), but doesn't work with deeper nesting (an array of objects within an array of objects).
My goal is to iterate through the feeds[0].feed.details.place
array and find the corresponding
cities.CountyPlaces.PlaceFIPSCode
value for each entry. I require the entire "place" object so that I can utilize any data within it 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 version of the data I am working with, showcasing the identical nesting structure:
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',
},
],
},
];