gpt4 book ai didi

javascript - 我的 o/p 中有两个数组。一个是结果数组,另一个是数据数组如何将两个数组合并为一个与坐标匹配的数组

转载 作者:行者123 更新时间:2023-11-30 11:19:00 25 4
gpt4 key购买 nike

如何将两个数组合并为一个数组,使第一个数组的坐标与第二个数组的 originToDestinedpointDirectionUrl 纬度和经度相匹配?

我使用了 object.assign 并合并了两个数组,但我没有看到结果数组和数据数组中的坐标匹配

 o/p:
{
"result": [
{
"id": "2X56cPnr0mvS9Cq8MAW1jQ",
"alias": "the-hot-tomato-fruita",
"name": "The Hot Tomato",
"coordinates": {
"latitude": 39.1594399,
"longitude": -108.732141
}
},
{
"id": "bGNVfuaRoIpPdUKsfROfZQ",
"alias": "camillas-kaffe-fruita",
"name": "Camilla's Kaffe",
"coordinates": {
"latitude": 39.1588374652023,
"longitude": -108.732291567344
}
},
{
"id": "jZtuJxbKfS8s4QCe_TrTxw",
"alias": "colorado-national-monument-fruita",
"name": "Colorado National Monument",
"coordinates": {
"latitude": 39.097564,
"longitude": -108.7387526
}
}

],
"data": [
{
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.097564,-108.7387526"
},
{
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.1594399,-108.732141"
},
{
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.1588374652023,-108.732291567344"
}

]

预计开工时间:

{
"third array": [
{
"id": "2X56cPnr0mvS9Cq8MAW1jQ",
"alias": "the-hot-tomato-fruita",
"name": "The Hot Tomato",
"coordinates": {
"latitude": 39.1594399,
"longitude": -108.732141
},
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.1594399,-108.732141"

},
{
"id": "bGNVfuaRoIpPdUKsfROfZQ",
"alias": "camillas-kaffe-fruita",
"name": "Camilla's Kaffe",
"coordinates": {
"latitude": 39.1588374652023,
"longitude": -108.732291567344
},
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.1588374652023,-108.732291567344"

},
{
"id": "jZtuJxbKfS8s4QCe_TrTxw",
"alias": "colorado-national-monument-fruita",
"name": "Colorado National Monument",
"coordinates": {
"latitude": 39.097564,
"longitude": -108.7387526
},
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.097564,-108.7387526"

}

]

最佳答案

let op = {
"result": [
{
"id": "2X56cPnr0mvS9Cq8MAW1jQ",
"alias": "the-hot-tomato-fruita",
"name": "The Hot Tomato",
"coordinates": {
"latitude": 39.1594399,
"longitude": -108.732141
}
},
{
"id": "bGNVfuaRoIpPdUKsfROfZQ",
"alias": "camillas-kaffe-fruita",
"name": "Camilla's Kaffe",
"coordinates": {
"latitude": 39.1588374652023,
"longitude": -108.732291567344
}
},
{
"id": "jZtuJxbKfS8s4QCe_TrTxw",
"alias": "colorado-national-monument-fruita",
"name": "Colorado National Monument",
"coordinates": {
"latitude": 39.097564,
"longitude": -108.7387526
}
}

],
"data": [
{
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.097564,-108.7387526"
},
{
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.1594399,-108.732141"
},
{
"originToDestinedpointDirectionUrl": "https://www.google.com/maps/dir/Los Angeles, CA, USA/39.1588374652023,-108.732291567344"
}
] };

let resultArr = op.result;
let dataArr = op.data;

let thirdArray = [];

dataArr.forEach(data => {
let splittedArr = data.originToDestinedpointDirectionUrl.split("/");
let coordinate = splittedArr[splittedArr.length-1].split(",");

let corLat = coordinate[0];
let corLong = coordinate[1];

resultArr.forEach(res => {

if(res.coordinates.latitude == corLat && res.coordinates.longitude == corLong){
thirdArray.push({ "id": res.id, "alias": res.alias, "name": res.name, "coordinates": res.coordinates, "originToDestinedpointDirectionUrl": data })
}
});
});

console.log(thirdArray);

为了获取坐标并在之后进行比较,涉及到一些字符串拆分。如果这不是您要找的,请告诉我

关于javascript - 我的 o/p 中有两个数组。一个是结果数组,另一个是数据数组如何将两个数组合并为一个与坐标匹配的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50521941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com