javascript 如何将属性添加到 angularjs 中的对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32700811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add properties to array of objects in angularjs
提问by rolser
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?
有人可以帮我做到这一点吗?
回答by Diego Unanue
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:
看看这个plunker:
回答by Shaishab Roy
You can't use push
to set property. push
can use to add array element.
If you want to use
您不能用于push
设置属性。push
可用于添加数组元素。如果你想使用
$scope.object.array1.push(value);
$scope.object.array1.push(value);
then value should be an object like
那么 value 应该是一个对象,比如
var value = {content1: 'xyz',id:'4'};
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. you can use forEach
to add new property and value. say called a function addProperty
to add property and value.
如果你想为name
array1 的每个对象添加属性。您可以使用forEach
添加新的属性和值。说调用了一个函数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"}]
回答by Denis Evseev
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.
您可以使用将发送insertName的函数。
回答by rolser
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);
i get an error saying: cannot read push.
当我尝试时,$scope.object.array1.push(value);
我收到一条错误消息:无法读取推送。
回答by lintmouse
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 等。