如何按坐标匹配合并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
- Build a Coordinate-to-URL Lookup Map: Extract the latitude/longitude pairs from each
originToDestinedpointDirectionUrlin thedataarray, 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. - Merge Result Items with Matching URLs: Iterate over each item in the
resultarray, generate the same coordinate string from itscoordinatesproperty, 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
|| nullensures that if a coordinate fromresultdoesn't exist indata, 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
resultand m is the length ofdata)—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
相关产品推荐
相关产品推荐

