jQuery 修改对象数组中的对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16691833/
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
Modify object property in an array of objects
提问by Johan
var foo = [{ bar: 1, baz: [1,2,3] }, { bar: 2, baz: [4,5,6] }];
var filtered = $.grep(foo, function(v){
return v.bar === 1;
});
console.log(filtered);
Is there any way to modify a certain objects property (like the one I'm filtering out above) without creating new arrays and/or objects?
有没有办法在不创建新数组和/或对象的情况下修改某个对象属性(就像我上面过滤掉的那个)?
Desired result: [{ bar: 1, baz: [11,22,33] }, { bar: 2, baz: [4,5,6] }]
想要的结果: [{ bar: 1, baz: [11,22,33] }, { bar: 2, baz: [4,5,6] }]
采纳答案by T.J. Crowder
Sure, just change it:
当然,只需更改它:
With jQuery's $.each
:
使用 jQuery 的$.each
:
$.each(foo, function() {
if (this.bar === 1) {
this.baz[0] = 11;
this.baz[1] = 22;
this.baz[2] = 33;
// Or: `this.baz = [11, 22, 33];`
}
});
With ES5's forEach
:
使用 ES5 forEach
:
foo.forEach(function(obj) {
if (obj.bar === 1) {
obj.baz[0] = 11;
obj.baz[1] = 22;
obj.baz[2] = 33;
// Or: `obj.baz = [11, 22, 33];`
}
});
...or you have other looping options in this other SO answer.
...或者您在这个其他 SO 答案中有其他循环选项。
回答by Suraj Rao
回答by David Barker
Without jQuery and backwards compatibility
没有 jQuery 和向后兼容性
for (var i = 0; i < foo.length; i++) {
if (foo[i].bar === 1) {
foo[i].baz = [11,12,13];
}
}
回答by Mahima Agrawal
We can also achieve this by using Array's map function:
我们也可以通过使用 Array 的 map 函数来实现:
foo.map((obj) => {
if(obj.bar == 1){
obj.baz[0] = 11;
obj.baz[1] = 22;
obj.baz[2] = 33;
}
})
回答by SivaRajini
$.each(foo,function(index,value)
{
if(this.bar==1)
{
this.baz[0] = 11;
this.baz[1] = 22;
this.baz[2] = 33;
}
});
but for loop is faster than $.each so u can try to use
但是 for 循环比 $.each 快,所以你可以尝试使用
for(var i=0; i <foo.length; i++)
{
if(foo[i].bar==1)
{
//change the code
}
}
回答by Piotr Pi?tkiewicz
.map
with spread(...
) operator
.map
使用扩展( ...
) 运算符
var result = foo.map(el => el.bar == 1 ? {...el, baz: [11,22,33]} : el);
var result = foo.map(el => el.bar == 1 ? {...el, baz: [11,22,33]} : el);
回答by Divyanshu Rawat
But before opting any of the mentioned techniques please keep in mind the performance challenges associated with each of the approach.
但在选择任何提到的技术之前,请记住与每种方法相关的性能挑战。
Object iterate For-In, average: ~240 microseconds.
Object iterate Keys For Each, average: ~294 microseconds.
Object iterate Entries For-Of, average: ~535 microseconds.
Reference - 3 JavaScript Performance Mistakes You Should Stop Doing
回答by Prashant Devs
You can make use of filter function of javascript.
您可以利用javascript的过滤功能。
obj = [
{inActive:false, id:1},
{inActive:false, id:2},
{inActive:false, id: 3}
];
let nObj = obj.filter(ele => {
ele.inActive = true;
return ele;
});
console.log(nObj);
回答by Muhammed Moussa
you can play around:
你可以玩:
const tasks = [ { id: 1, done: false }, { id: 2, done: false } ]
const completed_task = { id: 1, done: true }
const markCompleted = (tasks, task) => {
const index = tasks.findIndex(t => t.id === task.id);
tasks.splice(index, 1);
tasks.push(task);
return tasks;
}
console.log(tasks)
console.log(markCompleted(tasks, completed_task))
EDIT
编辑
to avoid index change:
避免索引更改:
const markCompleted = (tasks, task) => {
const index = tasks.findIndex(t => t.id === task.id);
tasks[index] = task;
return tasks;
}
回答by Kalpesh Dabhi
You can modify the array by using simple for loop
您可以使用简单的 for 循环修改数组
var foo = [{ bar: 1, baz: [1,2,3] }, { bar: 2, baz: [4,5,6] }];
for(i = 0;i < foo.length;i++){
//Here your confition for which item you went to edit
if(foo[i].bar == 1){
//Here you logic for update property
foo[i].baz= [1,11,22]
}
}
console.log(foo);