简体   繁体   English

如何在angularjs中向对象数组添加属性

[英]how to add properties to array of objects in angularjs

I have an array of objects as follows: 我有一个对象数组,如下所示:

var array = [{'content1': 'abc', 'id1':'100'},{'content2': 'xyz', id2':'200'}...];

Now I want to add new elements(I don't know if I can, but not manually ) to each of these objects and I want the same array to be as follows: 现在,我想向每个对象添加新元素(我不知道是否可以, 但不是手动添加 ),并且我希望相同的数组如下所示:

var array = [{'content1': 'abc', 'id1':'100', 'name': 'foo'},{'content2': 'xyz', 'id2':'200', 'name': 'bar'}...];

Can someone please help me to do this? 有人可以帮我做到这一点吗?

You can use angular merge 您可以使用角度合并

   var array = [{
    'content1': 'abc',
    'id1': '100'
  }, {
    'content2': 'xyz',
    'id2': '200'
  }];

  var array2 = [{
     'name': 'foo'
  }, {
     'name': 'bar'
  }];

  var object = angular.merge({}, array, array2)

and the result will be all properties merged: 结果将合并所有属性:

var array2 = [{
    'content1': 'abc',
    'id1': '100',
    'name': 'foo'
  }, {
    'content2': 'xyz',
    'id2': '200',
    'name': 'bar'
  }];

Take a look at this plunker: 看看这个矮人:

http://plnkr.co/edit/X7Zv99qNXSkmHh4lIBMP http://plnkr.co/edit/X7Zv99qNXSkmHh4lIBMP

You can't use push to set property. 您不能使用push来设置属性。 push can use to add array element. push可以用来添加数组元素。 If you want to use 如果要使用

$scope.object.array1.push(value);

then value should be an object like 那么值应该是一个像

var value = {content1: 'xyz',id:'4'};

To add property you can follow bellow process. 要添加属性,您可以按照以下步骤进行。

Say your object like: 像说您的对象:

$scope.object={
     array1: [{content1: 'abc', id1:'100'},
       {content2: 'xyz', id2:'200'}],
     array2: [{content1: 'abc', id1:'100'},
     {content2: 'xyz', id2:'200'}]
    };

and if you want to add property name for each object of array1. 如果要为array1的每个对象添加属性name you can use forEach to add new property and value. 您可以使用forEach添加新的属性和值。 say called a function addProperty to add property and value. 说一个叫做addProperty的函数来添加属性和值。

$scope.addProperty = function(){
  var i =0;
  angular.forEach($scope.object.array1, function(eachObj) {
    i+=1;
    eachObj.name ='foo'+i;
  });
};

then out put of array1 will be 然后把array1放出来

[{"content1":"abc","id1":"100","name":"foo1"},{"content2":"xyz","id2":"200","name":"foo2"}]

PLUNKER DEMO 朋克演示

The easiest way it is using .map(); 最简单的方法是使用.map();。

array.map(function(element) {
    return element.name = insertedName;
});

You can use a function which will send insertedName . 您可以使用将发送insertedName的函数。

There are several ways to skin this. 有几种方法可以对此进行皮肤处理。 Just depends how dynamic you need it to be. 取决于您需要的动态程度。

Here is a basic example showing how it can be done by accessing your objects by index: 这是一个基本示例,显示如何通过按索引访问对象来完成此操作:

var array = [{'content1': 'abc', 'id1':'100'},{'content2': 'xyz', 'id2':'200'}];

array[0].name = 'foo';
array[1].name = 'bar';

https://jsfiddle.net/0f6L1x44/ https://jsfiddle.net/0f6L1x44/

If you need to add names more dynamically, you could do something like: 如果您需要更动态地添加名称,则可以执行以下操作:

findById(100).name = 'foo';
findById(200).name = 'bar';

function findById(id) {
    var index = array.map(function(item) { return item.id; }).indexOf(id);

    return array[index];
}

https://jsfiddle.net/pj31n0Lj/ https://jsfiddle.net/pj31n0Lj/

Note: I cleaned up your array objects in this example. 注意:在此示例中,我清理了您的数组对象。 Not sure why you have content1 and content2, etc. 不知道为什么会有content1和content2等。

I have an object like: 我有一个像这样的对象:

$scope.object={array1: [ ], array2:[ ] };

How can I push new values into those arrays? 如何将新值推入这些数组?

When I try $scope.object.array1.push(value); 当我尝试$scope.object.array1.push(value); i get an error saying: cannot read push. 我收到一条错误消息: 无法读取推送。

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

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