javascript - AngularJS : How to concat two arrays? - Stack Overflow

I have following arrays with values (i am generating the values on go) $scope.objectName = [{ Name: �

I have following arrays with values (i am generating the values on go)

$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];

I want to concatenate these two objects to get the following result

[{Name:''},{ Key: '', Value: '' }]

Plunker link , somehow that's not working either

when I click on the add row button it will add another row for key and value text boxes only not for name, I can add n no of rows and when I click on Submit it should show the kev value pair as

[{Name:''},{ Key: '', Value: '' },{ Key: '', Value: '' },{ Key: '', Value: '' }.....so on]

Thanks

I have following arrays with values (i am generating the values on go)

$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];

I want to concatenate these two objects to get the following result

[{Name:''},{ Key: '', Value: '' }]

Plunker link , somehow that's not working either

when I click on the add row button it will add another row for key and value text boxes only not for name, I can add n no of rows and when I click on Submit it should show the kev value pair as

[{Name:''},{ Key: '', Value: '' },{ Key: '', Value: '' },{ Key: '', Value: '' }.....so on]

Thanks

Share Improve this question asked May 22, 2014 at 19:09 eightynineeightynine 1271 gold badge1 silver badge13 bronze badges 1
  • 1 Uh.... why do you want the name in the same array as your key value pairs. Couldn't you submit it like this: var submission = {}; submission.name = $scope.objecName.name; submission.keyValuePairs = $scope.propertiesElement; – Mike Quinlan Commented May 22, 2014 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 2

Not sure why you want to build an array of mismatched objects. That seems to me to be asking for trouble. I would suggest possibly doing the following:

$scope.objects = [{Name: '', Elements: []}];

Then you can easily manage multiple objects who have elements:

(I use underscore http://underscorejs/)

$scope.addElementToObject = function(objName, element){
   _.where($scope.mergedArray, {Name: objName}).Elements.push(element);
};

Then you can add to the list of elements for that object without having to eval the object in the elements array on each use.


If you still want/need to merge arrays of mismatched objects, it would be the following:

$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];
$scope.mergedArray = $scope.objectName.contact($scope.propertiesElement);

$scope.addElement = function(element){
   $scope.mergedArray.push(element);
};

Then, in your click event code:

$scope.addElement({ Key: 'someKey', Value: 'Some Value' });

I hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745519644a4631199.html

相关推荐

  • javascript - AngularJS : How to concat two arrays? - Stack Overflow

    I have following arrays with values (i am generating the values on go) $scope.objectName = [{ Name: �

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信