简体   繁体   English

比较两个不同的对象数组,并在 API 调用后找到匹配项时将数据推送到新对象

[英]Comparing two different arrays of objects and pushing data to a new object when a match is found after API Call

I am trying to create a map of COVID hotspots across the US.我正在尝试创建美国各地的 COVID 热点地图。 The array of objects I am getting from an API does not include lat and lng positioning so I created my own array to pass these values into the returned array with a .then statement.我从 API 获取的对象数组不包括 lat 和 lng 定位,因此我创建了自己的数组,以使用 .then 语句将这些值传递到返回的数组中。 The issue that I am having is that the array that I created is not allowing me to run a .forEach function on it and push the values to a new (third) array.我遇到的问题是我创建的数组不允许我在其上运行 .forEach 函数并将值推送到新的(第三个)数组。 Below is my code and a small sample of each of the arrays.下面是我的代码和每个数组的一个小样本。 I got the Lat / Lng info from here https://inkplant.com/code/us-state-mysql-table .我从这里获得了 Lat / Lng 信息https://inkplant.com/code/us-state-mysql-table

My Array:我的阵列:

let StateLatLng = [ 
{state: "Alabama", lat: 32.806671, lng: -86.791130},
{state: "Alaska", lat: 61.370716, lng: -152.404419},
{state: "Arizona", lat: 33.729759, lng: -111.431221},
{state: "Arkansas", lat: 34.969704, lng: -92.373123},
etc... ]

Array returned from API:从 API 返回的数组:

0: Object { state: "California", updated: 1603065757213, cases: 875557, … }
1: Object { state: "Texas", updated: 1603065757213, cases: 870156, … }
2: Object { state: "Florida", updated: 1603065757213, cases: 755020, … }
3: Object { state: "New York", updated: 1603065757213, cases: 519994, … }

My Code:我的代码:

.then((result) => {
  let data = new Set(result.map((item) => item.state));
  StateLatLng.forEach((item) data.has(item.state) && (item.lat = test);
  console.log(data);
})

I am attempting to use the shared state name for both arrays to find the matching object and then push the data onto the new array called data for use later.我正在尝试使用两个数组的共享状态名称来查找匹配的对象,然后将数据推送到名为 data 的新数组上供以后使用。 Since I created the first array I am not sure if I should format it differently t make the job easier or if I should use the MySQL table that is linked in the URL that I got the lat/lng from.由于我创建了第一个数组,我不确定我是否应该对其进行不同的格式化以使工作更容易,或者我是否应该使用链接在我从中获得经纬度/经纬度的 URL 中的 MySQL 表。 I opted not to use the SQL table because I am unfamiliar with that data type.我选择不使用 SQL 表,因为我不熟悉该数据类型。

Your code in your .then function is illogical.您的.then函数中的代码不合逻辑。 If I wanted the result Array to contain your latitudes and longitudes, I would do like:如果我希望结果数组包含您的纬度和经度,我会这样做:

.then(result=>{
  for(let o of result){
    for(let p of StateLatLng){
      if(o.state === p.state){
        o.lat = p.lat; o.lng = p.lng; // probably really just want to do your map stuff here
      }
    }
  }
  // result Array now has latitudes and longitudes 
});

This is what I tried with you code and sample data.这是我用你的代码和示例数据尝试过的。

let StateLatLng = [ 
    {state: "Alabama", lat: 32.806671, lng: -86.791130},
    {state: "Alaska", lat: 61.370716, lng: -152.404419},
    {state: "Arizona", lat: 33.729759, lng: -111.431221},
    {state: "Arkansas", lat: 34.969704, lng: -92.373123}
];

let results = [
    { state: "Alabama", updated: 1603065757213, cases: 875557 },
    { state: "Alaska", updated: 1603065757213, cases: 870156 },
    { state: "Arizona", updated: 1603065757213, cases: 755020 },
    { state: "Arkansas", updated: 1603065757213, cases: 519994 }
]

let newStateLatLng = [], data = [];

StateLatLng.map(item => {
    newStateLatLng[item['state']] = item;
})

results.forEach(item => {
    if (item.state && newStateLatLng[item.state]) {
        item['lat'] = newStateLatLng[item.state].lat;
        item['lng'] = newStateLatLng[item.state].lng;
        data.push(item);
    }
})

console.log(data);

The result is结果是

[
  {
    state: 'Alabama',
    updated: 1603065757213,
    cases: 875557,
    lat: 32.806671,
    lng: -86.79113
  },
  {
    state: 'Alaska',
    updated: 1603065757213,
    cases: 870156,
    lat: 61.370716,
    lng: -152.404419
  },
  {
    state: 'Arizona',
    updated: 1603065757213,
    cases: 755020,
    lat: 33.729759,
    lng: -111.431221
  },
  {
    state: 'Arkansas',
    updated: 1603065757213,
    cases: 519994,
    lat: 34.969704,
    lng: -92.373123
  }
]

It can be a one-liner它可以是单线

The key observation is that you are ALWAYS looking up the state in your self-made array.关键的观察是你总是在你自制的数组中查找状态。 So why not make it an object, with the state as the key?那么为什么不把它变成一个对象,以状态为键呢? You made it yourself, so you are free to do so.你自己做的,所以你可以自由地这样做。

This means you don't have to "manually" search for the state in your array.这意味着您不必“手动”搜索数组中的状态。 If you have a state name, you can just tell Javascript to look at that state's data.如果你有一个州名,你可以告诉 Javascript 查看该州的数据。

You can simply use results.map to convert the original stateResult into a new object that consist of the original stateResult plus the relevant element of your self-made LatLng object, using: ({...StateLatLng[stateResult.state],...stateResult})您可以简单地使用results.map将原始stateResult转换为一个新对象,该对象由原始 stateResult加上您自制的 LatLng 对象的相关元素组成,使用: ({...StateLatLng[stateResult.state],...stateResult})

 let StateLatLng = { "Alabama": { lat: 32.806671, lng: -86.791130 }, "Alaska": { lat: 61.370716, lng: -152.404419 }, "Arizona": { lat: 33.729759, lng: -111.431221 }, "Arkansas": { lat: 34.969704, lng: -92.373123 }, } let results = [{ state: "Alabama", updated: 1603065757213, cases: 875557 }, { state: "Alaska", updated: 1603065757213, cases: 870156 }, { state: "Arizona", updated: 1603065757213, cases: 755020 }, { state: "Arkansas", updated: 1603065757213, cases: 519994 } ] console.log( results.map( stateResult => ({ ...StateLatLng[stateResult.state], ...stateResult }) ) )

The result is as follows:结果如下:

 [ { "lat": 32.806671, "lng": -86.79113, "state": "Alabama", "updated": 1603065757213, "cases": 875557 }, { "lat": 61.370716, "lng": -152.404419, "state": "Alaska", "updated": 1603065757213, "cases": 870156 }, { "lat": 33.729759, "lng": -111.431221, "state": "Arizona", "updated": 1603065757213, "cases": 755020 }, { "lat": 34.969704, "lng": -92.373123, "state": "Arkansas", "updated": 1603065757213, "cases": 519994 } ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM