3

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]

4 Answers 4

2

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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"});
 }
}

Comments

0

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]);
}

2 Comments

i have number of records and data get from services dynamically.
You can push the data using loop.
0

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' }, ..]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.