简体   繁体   English

如何连接或合并两个数组

[英]How to concatenate or merge two arrays

I have two array variables: var1 and var2 . 我有两个数组变量: var1var2

$scope.var1 = {
"0":{"subjectId":"1","subjectShortName":"English","subjectCode":null,"IsSelected":true},
"1":{"subjectId":"2","subjectShortName":"Hindi","subjectCode":null,"IsSelected":true}
};

$scope.var2 = {"classId":"3","typeOfSubject":"main"}

How I can merge or concatenate these two arrays to get the kind of array below? 我如何合并或连接这两个数组以获得下面的那种数组? Basically I want to add all subjectId as Key to second array. 基本上我想将所有subjectId添加为第二个数组的Key。

$scope.var2 = {"classId":"3","typeOfSubject":"main", "subjectId":"{1,2}"}

In your exemple you are using objects not arrays. 在你的例子中,你使用的是对象而不是数组。 But I leave here how to join two objects. 但我离开这里如何加入两个对象。 https://docs.angularjs.org/api/ng/function/angular.extend https://docs.angularjs.org/api/ng/function/angular.extend

In your case: 在你的情况下:

$scope.new_var = angular.extend({}, $scope.var1, $scope.var2)

.

If you intend to use array I do not know a native function in angularjs. 如果你打算使用数组,我不知道angularjs中的原生函数。

You can use jquery > http://api.jquery.com/jquery.merge/ 你可以使用jquery > http://api.jquery.com/jquery.merge/

Or javascript 或者javascript

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

Your expectation is not clear. 你的期望不明确。 Anyway following your request here it is. 无论如何,按照你的要求,它是。 It will add the subjectIds of var1 into var2 as a list of integers. 它会将var1的subjectIds作为整数列表添加到var2中。

var subjectIds = [];
for(var k in $scope.var1) {
   subjectIds.push(parseInt($scope.var1[k].subjectId));
}
$scope.var2.subjectId = subjectIds.join(', ');

Results into 结果成

{ classId: '3', typeOfSubject: 'main', subjectId: "1, 2" }

It looks like you want to merge these objects so the result looks like: 看起来您想要合并这些对象,结果如下所示:

{
    "classId": "3", 
    "typeOfSubject": "main", 
    "subjectId": {
        "1": {"subjectId": "1", "subjectShortName": "English", ...}
        "2": {"subjectId": "2", "subjectShortName": "Hindi", ...}
    }
}

This doesn't require any angular support to accomplish, but you can use angular.forEach to iterate your var1 object easily: 这不需要任何角度支持,但您可以使用angular.forEach轻松迭代var1对象:

$scope.var2.subjectId = {};

angular.forEach($scope.var1, function(subject){
    this[subject.subjectId] = subject;
}, $scope.var2.subjectId); //set "this" to the member of var2 we're populating

These are objects, not array.. note that array have this [] brackets, while objects are {} just like your code.. in this case you want to join a new object into your constructed object.. 这些是对象,而不是数组..请注意,数组有这个[]括号,而对象就像你的代码一样.. {}在这种情况下,你想要将一个新对象加入到你构造的对象中。

Your problem has already been solved in this post here 你的问题已经在这篇文章中解决

But if you wish to see a smaller code.. would be using extends function in AngularJS according to the docs here 但是如果你希望看到一个更小的代码..根据这里的文档,将在AngularJS使用extends函数

angular.extends(yourObject, objectToJoin, ...);

note that this function is inherited from jQuery and you can check the jQuery docs version in here 请注意,此函数继承自jQuery ,您可以在此处查看jQuery文档版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM