[英]How to traverse through an array of objects and find similar values in them in 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)
您可以使用
Array#flatMap
提取所有subObjs
項的數組
Array#map
僅將它們映射到其父級的id
屬性中。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.