admin管理员组

文章数量:1389356

This is my code

$scope.studentDetails=[];

$scope.studentDetails=[0][id:101,name:one]
                      [1][id:102,name:two]
                      [2][id:103,name:three] 

$scope.studentMarks=[];

$scope.studentMarks=[0][id:101,marks:78]
                    [1][id:102,marks:89]

i have two arrays,first array contains 2 properties like id and name, second array contains two properties like id and marks,i want to concatinate these two arrays into one array.i want to get output like

$scope.studentDetails=[0][id:101,name:one,marks:78]
                      [1][id:102,name:two,marks:89]
                      [2][id:103,name:three,marks:null]

This is my code

$scope.studentDetails=[];

$scope.studentDetails=[0][id:101,name:one]
                      [1][id:102,name:two]
                      [2][id:103,name:three] 

$scope.studentMarks=[];

$scope.studentMarks=[0][id:101,marks:78]
                    [1][id:102,marks:89]

i have two arrays,first array contains 2 properties like id and name, second array contains two properties like id and marks,i want to concatinate these two arrays into one array.i want to get output like

$scope.studentDetails=[0][id:101,name:one,marks:78]
                      [1][id:102,name:two,marks:89]
                      [2][id:103,name:three,marks:null]
Share Improve this question asked Oct 12, 2015 at 4:51 durga siva kishore mopurudurga siva kishore mopuru 1,3476 gold badges34 silver badges57 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Lodash zip() should do that provided your JavaScript is valid in the first place.

$scope.studentDetails = _.zip($scope.studentDetails, $scope.studentMarks);

I got the answer

var newArray = [];
_.each($scope.studentDetails,function(obj))
{
 var data=_.findWhere($scope.studentMarks,{"id":obj.id});
 if(!_.isUndefined(data))
 {
   newArray.push({id:obj.id,name:obj.name,marks:data.marks});
 }
else
 {
newArray.push({id:obj.id,name:obj.name,marks:"null"});
 }
}

Hey you can use the push like

$scope.studentDetails.push({'id':'101','name':'one','marks':'78'});
$scope.studentDetails.push({'id':'102','name':'two','marks':'78'});
$scope.studentDetails.push({'id':'103','name':'three','marks':'78'});

using loop you can append like bellow

for(i = 0; i < studentResult.length; i++){
    $scope.studentDetails.push(studentResult[i]);
}

For object array _.zip merged two array into single array where each array element also an array.

You can use .map and .extend to create merged object array with _.zip like

var studentDetails = [{ id: 101, name: 'one' }, { id: 102, name: 'two' }, { id: 103, name: 'three' }];
var studentMarks = [{ id: 101, marks: 78 }, { id: 102, marks: 89 }];

var mergedArray = _.zip(studentDetails, studentMarks); //where each element also an array like [ [{ id: 101, name: 'one' }, { id: 101, marks: 78 }] ]

var studentDetails = _.map(mergedArray, function (item) { return _.extend(item[0], item[1]); }); //[{ id: 101, marks: 78, name: 'one' }, ..]

本文标签: javascriptHow to merge two arrays into one array in angularjsStack Overflow