如何实现基于特定对象ID序列的数组拼接,插入中间匹配对象
Solution: Merge Arrays with Intermediate Elements Insertion
Here's a straightforward approach to achieve your desired array merging logic:
Approach
- Map IDs to their positions in the second array: First, create a lookup map that associates each object ID from
secondArraywith its index. This lets us quickly find where each ID is located without searching the array repeatedly. - Build the result array: Iterate through
firstArray, adding each element to the result. For each pair of adjacent elements infirstArray, extract all elements fromsecondArraythat 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
idToIndexmap lets us get the position of any ID insecondArrayin O(1) time, making the solution efficient even for larger arrays. - Slice and Insert: For each adjacent pair in
firstArray, we useslice()to get elements fromsecondArraythat are between the two IDs. Sinceslice()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
firstArrayis fully maintained, and the intermediate elements fromsecondArrayare inserted in their original sequence as well.
Notes
- This solution assumes that every ID in
firstArrayexists exactly once insecondArray, and that the sequence of these IDs insecondArraymatches their order infirstArray(which aligns with your example). - If an ID from
firstArraymight be missing insecondArray, 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
相关产品推荐
相关产品推荐

