Bootstrap

JS 两个数组对象通过相同的id 一对多 合并成一个数组对象

注意:arr1 是基础 把arr2合并到arr1中 ,汇成一个总的arrs 这个arr1中的id 和 arr2中的periodId 是一一对应的。

<script>
     let arr1 = [
                {id:64,period:"10:00"},
                {id:65,period:"12:00"},
                {id:66,period:"14:00"},
                {id:67,period:"18:00"},
            ]
    let arr2 = [
                {periodId:64,name:"张三",age:16},
                {periodId:64,name:"李四",age:17},
                {periodId:64,name:"王五",age:18},
                {periodId:64,name:"赵六",age:19},
                {periodId:64,name:"小七",age:20},
                {periodId:65,name:"王五",age:18},
                {periodId:65,name:"赵六",age:19},
                {periodId:65,name:"小七",age:20},
            ]
 const arrs = arr1.map(item => {
    const data = arr2.filter(i => item.id == i.periodId)
    return {
      ...item,
      products: data 
    }
  })
console.log(arrs)

</script>

在这里插入图片描述

;