You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按坐标匹配合并result与data两个数组为单个数组?

Solution: Merge Arrays by Matching Coordinates from URLs

Got it, let's fix this coordinate-matching merge problem for you! Using Object.assign directly won't work here because it doesn't have built-in logic to link the two arrays via coordinates. We need to first create a lookup map from the data array, then merge that with the result items properly.

Step-by-Step Approach

  1. Build a Coordinate-to-URL Lookup Map: Extract the latitude/longitude pairs from each originToDestinedpointDirectionUrl in the data array, and store them in an object where the key is a string of the coordinates (e.g., "39.097564,-108.7387526"), and the value is the corresponding URL. This makes matching fast and efficient.
  2. Merge Result Items with Matching URLs: Iterate over each item in the result array, generate the same coordinate string from its coordinates property, look up the matching URL from our map, and merge the two objects together.

Code Implementation

// Your sample input data
const input = {
  "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" }
  ]
};

// Step 1: Create the coordinate-to-URL map
const coordinateUrlMap = {};
input.data.forEach(item => {
  // Extract the coordinate segment from the end of the URL
  const urlParts = item.originToDestinedpointDirectionUrl.split('/');
  const coordsKey = urlParts[urlParts.length - 1];
  coordinateUrlMap[coordsKey] = item.originToDestinedpointDirectionUrl;
});

// Step 2: Merge result items with their matching URLs
const thirdArray = input.result.map(resultItem => {
  // Format coordinates to match the map's key format
  const coordsKey = `${resultItem.coordinates.latitude},${resultItem.coordinates.longitude}`;
  // Merge the result item with the URL (add null if no match exists)
  return {
    ...resultItem,
    originToDestinedpointDirectionUrl: coordinateUrlMap[coordsKey] || null
  };
});

// Generate the final output
const output = { "third array": thirdArray };
console.log(JSON.stringify(output, null, 2));

Key Details

  • Robust Coordinate Extraction: The example uses split('/') to grab the last segment of the URL, which works perfectly for your input. If URLs might have trailing slashes or different structures, you could use a regex like /([-\d.]+),([-\d.]+)$/ to capture coordinates more reliably.
  • Handling Missing Matches: The || null ensures that if a coordinate from result doesn't exist in data, we still get a valid value (you can adjust this to skip the item or use a default URL if needed).
  • Efficiency: Using a lookup map means each match is O(1), making the whole process O(n + m) (where n is the length of result and m is the length of data)—way faster than nested loops.

Expected Output

Running the code above will produce exactly the output you're looking for:

{
  "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"
    }
  ]
}

内容的提问来源于stack exchange,提问作者RAHUL SRV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:43:16