Javascript:在深度嵌套的数组和对象中查找匹配的属性值

olhwl3o2  于 2023-04-10  发布在  Java
关注(0)|答案(2)|浏览(204)

我需要匹配来自两个JSON源的值。使用javascript find方法,当“cities”数组的嵌套更浅 (只是一个对象数组) 时,这种方法对我有效,但它不适用于更深的嵌套 (对象数组中的对象数组)
本质上,我试图做的是遍历feeds[0].feed.details.place数组并为每个数组找到匹配的cities.CountyPlaces.PlaceFIPSCode值。我实际上需要整个“place”对象,因此我可以为每个匹配使用其中的任何数据。

// 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);
  }
});

下面是我使用的数据的一个简化示例,具有相同的嵌套:

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',
      },
    ],
  },
];
uqzxnwby

uqzxnwby1#

如果我理解了你的问题,你能试试下面的代码吗?

const cityMatch = cities.find((cityObject, i) => {
    // console.log(i, 'cityObject: ', cityObject);
    return cityObject.CountyPlaces.some(p=>p.PlaceName===arrItem)
   
  });

获取唯一匹配位置

const placeMatch = cityMatch.CountyPlaces.filter(p=>p.PlaceName===arrItem)[0]
q5iwbnjs

q5iwbnjs2#

您可以根据CountyName匹配details.county[0]来过滤cities数组,然后根据PlaceNamedetails.place中过滤匹配城市的CountyPlaces

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',
      },
    ],
  },
];

const county = feeds[0].feed.details.county[0];
const places = feeds[0].feed.details.place;

const result = cities
  .filter(city => city.CountyName == county)[0]
  .CountyPlaces.filter(({ PlaceName }) => places.includes(PlaceName))
  
console.log(result)

相关问题