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

如何实现基于特定对象ID序列的数组拼接,插入中间匹配对象

Solution: Merge Arrays with Intermediate Elements Insertion

Here's a straightforward approach to achieve your desired array merging logic:

Approach

  1. Map IDs to their positions in the second array: First, create a lookup map that associates each object ID from secondArray with its index. This lets us quickly find where each ID is located without searching the array repeatedly.
  2. Build the result array: Iterate through firstArray, adding each element to the result. For each pair of adjacent elements in firstArray, extract all elements from secondArray that lie between their positions and insert those into the result.

Code Implementation

const firstArray = [
 { id: 'LOA13', type: 'LOAD', otherProperty: 11 },
 { id: 'UNA1', type: 'UNLOAD', otherProperty: 22 },
 { id: 'LOA14', type: 'LOAD', otherProperty: 33 },
 { id: 'UNA15', type: 'UNLOAD', otherProperty: 44 }
];

const secondArray = [
 { id: 'LOA13', type: 'LOAD', otherProperty: 12 },
 { id: '123', type: 'DRAG', otherProperty: 34 },
 { id: 'UNA1', type: 'UNLOAD', otherProperty: 56 },
 { id: 'LOA14', type: 'LOAD', otherProperty: 12 },
 { id: '456', type: 'DRAG', otherProperty: 1212 },
 { id: '789', type: 'DRAG', otherProperty: 9898 },
 { id: 'UNA15', type: 'UNLOAD', otherProperty: 56 }
];

// Create a map of ID to its index in secondArray
const idToIndex = new Map();
secondArray.forEach((item, index) => {
  idToIndex.set(item.id, index);
});

const result = [];

for (let i = 0; i < firstArray.length; i++) {
  // Add the current item from firstArray to the result
  result.push(firstArray[i]);
  
  // If we're not at the last element, add intermediate items from secondArray
  if (i < firstArray.length - 1) {
    const currentId = firstArray[i].id;
    const nextId = firstArray[i + 1].id;
    
    const currentPosition = idToIndex.get(currentId);
    const nextPosition = idToIndex.get(nextId);
    
    // Extract elements between current and next positions (exclusive of both)
    const intermediateItems = secondArray.slice(currentPosition + 1, nextPosition);
    result.push(...intermediateItems);
  }
}

console.log(result);

How It Works

  • Lookup Map: The idToIndex map lets us get the position of any ID in secondArray in O(1) time, making the solution efficient even for larger arrays.
  • Slice and Insert: For each adjacent pair in firstArray, we use slice() to get elements from secondArray that are between the two IDs. Since slice() is inclusive of the start index and exclusive of the end index, slice(currentPosition +1, nextPosition) gives exactly the elements we need to insert.
  • Preserve Order: The original order of firstArray is fully maintained, and the intermediate elements from secondArray are inserted in their original sequence as well.

Notes

  • This solution assumes that every ID in firstArray exists exactly once in secondArray, and that the sequence of these IDs in secondArray matches their order in firstArray (which aligns with your example).
  • If an ID from firstArray might be missing in secondArray, you can add error handling (like checking if the ID exists in the map before accessing its index) to avoid runtime errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 03:57:34