Merge two arrays matching an id

前端 未结 6 1297
死守一世寂寞
死守一世寂寞 2020-12-19 18:55

I have two arrays like

var members = [{docId: \"1234\", userId: 222}, {docId: \"1235\", userId: 333}];
var memberInfo = [{id: 222, name: \"test1\"}, {id: 333         


        
6条回答
  •  攒了一身酷
    2020-12-19 19:21

    Do a nested map and update the top level element with the additional field date where fields in the arrays match.

    member.map(mem => {
        return memberInfo.map(info => {
            if (info.id === mem.userId) {
                mem.date = info.date;
                return mem;
            }
        }
    }
    

提交回复
热议问题