Merge two arrays matching an id

前端 未结 6 1283
死守一世寂寞
死守一世寂寞 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:34

    You can achieve using foreach function and creating the third array and displaying it.

    $scope.members = [{docId: "1234", userId: 222}, {docId: "1235", userId: 333}];
    $scope.memberInfo = [{id: 222, name: "test1"}, {id: 333, name: "test2"}];
    $scope.finalArray = [];
    
    angular.forEach($scope.members, function(member) {
        angular.forEach($scope.memberInfo, function(memberInfo) {
          if(member.userId ==memberInfo.id) {
              var test = {
                docId : member.docId,
                userId: member.userId,
                name: memberInfo.name
              }
              $scope.finalArray.push(test);
          }
      });
    });
    

    Here is the working plunker:

    http://embed.plnkr.co/QRB5v2cI6SZOdZgdqDVR/preview

    Hope it helps!

提交回复
热议问题