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
我有两个数组,第一个数组包含两个属性id和name,第二个数组包含两个属性id和标记,我想把这两个数组合并到一个数组中。我想要输出
$scope.studentDetails=[0][id:101,name:one,marks:78]
[1][id:102,name:two,marks:89]
[2][id:103,name:three,marks:null]
4 个解决方案
#1
2
Lodash zip() should do that provided your JavaScript is valid in the first place.
Lodash zip()应该这样做,前提是您的JavaScript首先是有效的。
$scope.studentDetails = _.zip($scope.studentDetails, $scope.studentMarks);
美元的范围。studentDetails = _.zip(美元范围。studentDetails scope.studentMarks美元);
#2
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"});
}
}
#3
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]);
}
#4
0
For object array _.zip merged two array into single array where each array element also an array.
对象数组_。zip将两个数组合并到单个数组中,其中每个数组元素也是一个数组。
You can use .map and .extend to create merged object array with _.zip like
可以使用.map和.extend创建带有_的合并对象数组。zip像
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' }, ..]
#1
2
Lodash zip() should do that provided your JavaScript is valid in the first place.
Lodash zip()应该这样做,前提是您的JavaScript首先是有效的。
$scope.studentDetails = _.zip($scope.studentDetails, $scope.studentMarks);
美元的范围。studentDetails = _.zip(美元范围。studentDetails scope.studentMarks美元);
#2
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"});
}
}
#3
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]);
}
#4
0
For object array _.zip merged two array into single array where each array element also an array.
对象数组_。zip将两个数组合并到单个数组中,其中每个数组元素也是一个数组。
You can use .map and .extend to create merged object array with _.zip like
可以使用.map和.extend创建带有_的合并对象数组。zip像
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' }, ..]