其他分享
首页 > 其他分享> > js将数组内属性值相同的项合并成二维数组

js将数组内属性值相同的项合并成二维数组

作者:互联网

var ary=[
        {"RaDate":'2021-09-08',"Type":'Morning1','title':'测试1'},
        {"RaDate":'2021-09-08',"Type":'Morning2','title':'测试2'},
        {"RaDate":'2021-10-08',"Type":'Morning3','title':'测试3'},
        {"RaDate":'2021-09-08',"Type":'Morning4','title':'测试4'},
        {"RaDate":'2021-10-08',"Type":'Morning5','title':'测试5'},
        ] //原数组
    var arrs=[
    {"RaDate":'2021-09-08','child':[]},
    {"RaDate":'2021-10-08','child':[]},
    {"RaDate":'2021-11-08','child':[]},
    ] //新创建的数组
    for(let i=0;i<arrs.length;i++){
        var result = ary.reduce((cur, next,index) => {
        console.log(cur, next,index)
          var key = next.RaDate
          key ==arrs[i].RaDate ?cur.push(next): ''
          return cur;
      }, [])
      if(result.length==0){
        arrs.splice(i,1)
      }else{
        arrs[i].child=result
      }
      console.log(result)

    }
  console.log(arrs)

 

标签:title,08,js,二维,arrs,2021,数组,Type,RaDate
来源: https://www.cnblogs.com/anna001/p/16248202.html