JS 兩個數組對象通過相同的id 合並成一個數組對象


注意:arr1 是基礎 把arr2合並到arr1中 ,匯成一個總的arrs  這個arr1中的id 和  arr2中的periodId  是一一對應的。

let arr1 = [ {id:64,period:"10:00"}, {id:65,period:"12:00"}, {id:66,period:"14:00"}, {id:67,period:"18:00"}, ] let arr2 = [ { period:'10:00', periodId: 64, products:[ { goodName: "套餐測試A-001", goodSn: "1368017317954457600", id: 252, limitedNum: 1, productId: 1658, remark: null, seckillPeriodId: 64, seckillPrice: 1, seckillStock: 1, skuId: 2947, status: 4, stock: 97, unitPrice: 100, } ] } ] const arrs = arr1.map(item => { const data = arr2.find(i => item.id == i.periodId) return { ...item, ...data, products: data ? data.products : [] } }) console.log(arrs)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM