簡體   English   中英

如何遍歷 2 個 javascript 對象數組並創建第三個?

[英]How to traverse through 2 javascript array of objects and create a third one?

我有兩個對象。 obj2 有文本,obj1 有 3 個 subIbjId。 如何根據 obj1 將“id”添加到 obj2? 例如:obj1 在第一個對象(1001, 1002)中有 2 個 subObjId。 我想計算 obj1 擁有的 subObjId 的數量並遍歷 obj1 並將 id 的鍵和值添加到 obj2。 如果 obj1 有兩個 subObjId,則將 id: 1 添加到 obj2 的前兩個條目中,依此類推。

我正在學習 javascript 並試圖解決一些想象中的問題。 任何幫助將不勝感激。 謝謝你。

 var obj1 = { [ { id: 1, name: 'apple', subObjId: [ 1001, 1002] subObjs: [ { subId: 1001 subName: 'ant', }, { subId: 1002 subName: 'ball', } ], }, { { id: 2, name: 'zebra', subObjId: [ 1003] subObjs: [ { subId: 1003 subName: 'cat', } ], }, ] } var obj2 = { [ { text: 'i am a text' }, { text: 'i am some random characters' }, { text: 'i am some numbers' } ] } to become finalObject = { [ { id: 1, text: 'i am a text' }, { id: 1, text: 'i am some random characters' }, { id: 2, text: 'i am some numbers' } ] }

嘗試這個!!

 var obj1 = [ { id: 1, name: 'apple', subObjId: [ 1001, 1002] }, { id: 2, name: 'zebra', subObjId: [ 1003] } ] var obj2 = [ { text: 'i am a text' }, { text: 'i am some random characters' }, { text: ' am some numbers' } ] let index = 0; let start = 0; obj1.map(data1=>{ index += data1.subObjId.length; for(var i=start;i<index;i++){ obj2[i]['id'] = data1.id; } start = i; }) console.log(obj2)

您可以使用

  1. Array#flatMap提取所有subObjs項的數組
    • Array#map僅將它們映射到其父級的id屬性中。
  2. 執行另一個映射操作,將匹配的 object 的內容復制到obj2中並添加一個id
    • 為方便起見,這使用了Array#map的第二個參數,它在回調中設置了this上下文。

還使用解構來實現緊湊性和傳播語法來制作副本:

 var obj1 = [ { id: 1, name: 'apple', subObjId: [1001, 1002], subObjs: [ { subId: 1001, subName: 'ant' }, { subId: 1002, subName: 'ball' } ] }, { id: 2, name: 'zebra', subObjId: [1003], subObjs: [ { subId: 1003, subName: 'cat', } ], }, ]; var obj2 = [ { text: 'i am a text' }, { text: 'i am some random characters' }, { text: 'i am some numbers' } ]; var finalObject = obj1 //1. flatMap into a single array.flatMap(({id, subObjs}) => subObjs.map(sub => ({id})) //take only the parent ID for each sub object )// result: [{id: 1}, {id: 1}, {id: 2}].map(function({id}, index) { return { id, ...this[index] } //2. create a new object with the id and the content of a matching object from the other array }, obj2);// <- set the `this` context for the callback console.log(finalObject);

它也可以在平面映射時作為單個操作完成,方法是將this上下文設置為第二個數組的副本(以避免變異obj2 ),然后使用Array#shift從新數組的前面取出項目:

 var obj1 = [ { id: 1, name: 'apple', subObjId: [1001, 1002], subObjs: [ { subId: 1001, subName: 'ant' }, { subId: 1002, subName: 'ball' } ] }, { id: 2, name: 'zebra', subObjId: [1003], subObjs: [ { subId: 1003, subName: 'cat', } ], }, ]; var obj2 = [ { text: 'i am a text' }, { text: 'i am some random characters' }, { text: 'i am some numbers' } ]; var finalObject = obj1.flatMap(function({id, subObjs}) { return subObjs.map(sub => ({ id, ...this.shift() })) }, [...obj2]);// <- copy obj2 as the `this` context console.log(finalObject);

使用這個片段

 var obj1 = [ { id: 1, name: 'apple', subObjId: [1001, 1002], subObjs: [{ subId: 1001, subName: 'ant', }, { subId: 1002, subName: 'ball', } ], }, { id: 2, name: 'zebra', subObjId: [1003], subObjs: [{ subId: 1003, subName: 'cat', }], }, ] var obj2 = [ { text: 'i am a text' }, { text: 'i am some random characters' }, { text: 'i am some numbers' } ] var finalObject = obj1.map(function (value, index) { return { id: value.id, text: obj2[index].text } }) console.log(finalObject)

暫無
暫無

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

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