簡體   English   中英

在另一個數組中的對象內的一個對象數組中添加對象內容

[英]Add object contents in one object array within objects in another array

我有兩個包含對象數組的大文件,第一個包含這樣的數據:

[{
    "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.",
}, (etc...)

第二個,數據如下:

[{
    "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"
    }
}, (etc...)

我想組合這兩個文件,以便將第二個文件中的“meta”信息添加到第一個文件中的相應信息中,這樣:

[{
    "id": "001",
    "word": "abbess",
    "def": "(noun) The lady superior of a nunnery",
    "meta": {
        "term": "abbess",
        "part_of_speech": "noun",
        "definition": "The lady superior of a nunnery"
    }
}, {
    "id": "002"
    "word": "abbey - (noun) The group of buildings which collectively form the dwelling-place of a society of monks or nuns.",
    "def": "(noun) The group of buildings which collectively form the dwelling-place of a society of monks or nuns.",
    "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"
    }
}, (etc...)

現在,我有這個代碼

 var newArr = [];
 for(var i = 0; i < meta.length; i++) {
    newArr.push(words[i]);
    newArr.push(meta[i]);
 }

在對象之后添加元對象,而不是在對象之內。 我是否需要循環另一個圖層來在單詞對象中添加元對象,或者是否有一個不同的方法可以在這里更好地工作,比如.concat()?

如果每個數組中的每個元素對應於另一個數組中具有相同索引的另一個元素,則它是一個簡單的.map ,它比for循環更合適:

 const input1 = [{ "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.", }]; const input2 = [{ "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" } }]; const combined = input1.map((item) => { const { word } = item ; const foundInput2 = input2.find(({ meta: { term }}) => term === word); const { meta } = foundInput2; return { ...item, meta }; }); console.log(combined); 

循環遍歷元數組並使用Object.assign將元添加到第一個數組中的相應對象:

 var arr = [{ "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.", }] const arr2 = [{ "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" } }] arr2.forEach((e, i) => { Object.assign(arr[i], e); }); console.log(arr) 

如果陣列沒有.find ,您可以使用.map.find來實現目標。

 const input1 = [{ "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.", }]; const input2 = [{ "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" } }]; const output = input1.map(item => { return { ...item, ...input2.find(item2 => item2.meta.term === item.word) } }); console.log(output); 

只需從第一個數組中設置對象的新屬性即可。

 var newArr = [];
 for(var i = 0; i < meta.length; i++) {
    var word = words[i];
    word.meta = meta[i].meta;
    newArr.push(word);
 }

這假設兩個數組總是以相同的順序具有相同單詞的信息。

獎金提示 - 如果您使用ECMAScript 6,您可以連接這樣的對象:

 const newArr = [];
 for(let i = 0; i < meta.length; i++) {
    newArr.push({ ...words[i], ...meta[i]} );
 }

另一種方法是使用函數reduce + function map

函數reduce將第二個數組input2轉換為一個對象,其中鍵來自屬性meta.term ,這樣函數map使用該對象通過鍵非常快速地找到相應的元值而不是重復的find執行。

此方法獨立於訂單工作,因為它將匹配屬性word和屬性meta.term

 const input1 = [{ "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.",}], input2 = [{ "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" }}], mapped = input2.reduce((a, o) => Object.assign(a, {[o.meta.term]: o.meta}), {}), result = input1.map((o) => Object.assign({}, o, {meta: mapped[o.word]})); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM