如何将一个对象数组的内容添加至另一个数组的对象中?附示例文件
Got it, let's tackle this problem of combining two arrays of objects that have different structures. I’ll use JavaScript here since it’s commonly used for this kind of data manipulation, and I’ll cover both a straightforward method and an optimized version for your large files.
Basic Approach: Match and Merge with map + find
First, let’s start with a simple implementation that works well for smaller datasets, then scale it up for large files.
Step 1: Define Your Sample Arrays
Let’s use your provided structure as an example:
// First array (from your first file) const wordDefinitions = [ { "id": "001", "word": "abbess", "def": "(noun) The lady superior of a nunnery" }, { "id": "002", "word": "abbey", "def": "(noun) The group of buildings which collectively form the dwelling-place of a society of monks or nuns." } ]; // Second array (from your second file) const metaData = [ { "meta": { "term": "abbess", "part_of_speech": "noun", "definition": "The lady superior of a nunnery" } }, { "meta": { "term": "abbey", "part_of_speech": "noun", "definition": "The group of buildings which collectively form the dwelling-place of a society of monks or nuns." } } ];
Step 2: Merge the Arrays
We’ll iterate over the first array, find the matching entry in the second array using word/term as the key, and combine the properties:
// Merge meta data into the word definition objects const mergedArray = wordDefinitions.map(wordEntry => { // Find the matching meta entry const matchingMeta = metaData.find(metaEntry => metaEntry.meta.term === wordEntry.word); // If a match exists, combine properties; otherwise keep the original entry if (matchingMeta) { return { ...wordEntry, // Preserve all original properties part_of_speech: matchingMeta.meta.part_of_speech, detailed_definition: matchingMeta.meta.definition // Optional: add specific meta fields // Or spread all meta properties at once: ...matchingMeta.meta }; } return wordEntry; }); console.log(mergedArray);
Optimized Approach for Large Files
If your files are really large (think thousands of entries), the find method can get slow because it scans the entire second array for every entry in the first. Instead, we’ll create a lookup object first to make matching instant:
// Create a lookup object where keys are the terms from the second array const metaLookup = metaData.reduce((lookup, metaEntry) => { lookup[metaEntry.meta.term] = metaEntry.meta; return lookup; }, {}); // Now merge using the lookup (O(1) lookups instead of O(n)) const optimizedMergedArray = wordDefinitions.map(wordEntry => { const matchingMeta = metaLookup[wordEntry.word]; return matchingMeta ? { ...wordEntry, ...matchingMeta } : wordEntry; });
This cuts the time complexity from O(n²) to O(n), which is way more efficient for big datasets.
Customization Tips
- If you want to merge the first array’s data into the second array instead, just reverse the logic: iterate over
metaDataand match againstwordDefinitions. - Adjust the properties you add based on your needs—you can spread all meta properties or pick specific ones like
part_of_speech. - Handle cases where there’s no match (e.g., add a default value or flag) if needed.
内容的提问来源于stack exchange,提问作者R Christian

