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);
有没有办法在不创建新数组和/或对象的情况下修改某个对象属性(比如我上面过滤的属性)?
期望的结果:[{ bar: 1, baz: [11,22,33] }, { bar: 2, baz: [4,5,6] }]
答案 0 :(得分:21)
当然,只需改变它:
使用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];`
}
});
使用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];`
}
});
答案 1 :(得分:17)
.map
和 spread (...
)运算符
var result = foo.map(el => el.bar == 1 ? {...el, baz: [11,22,33]} : el);
答案 2 :(得分:8)
没有jQuery和向后兼容性
for (var i = 0; i < foo.length; i++) {
if (foo[i].bar === 1) {
foo[i].baz = [11,12,13];
}
}
答案 3 :(得分:6)
您可以使用find
并更改其属性。
let foo = [{ bar: 1, baz: [1,2,3] }, { bar: 2, baz: [4,5,6] }];
let obj = foo.find(f=>f.bar==1);
if(obj)
obj.baz=[2,3,4];
console.log(foo);
答案 4 :(得分:3)
我们也可以通过使用Array的map函数来实现这个目的:
foo.map((obj) => {
if(obj.bar == 1){
obj.baz[0] = 11;
obj.baz[1] = 22;
obj.baz[2] = 33;
}
})
答案 5 :(得分:1)
$.each(foo,function(index,value)
{
if(this.bar==1)
{
this.baz[0] = 11;
this.baz[1] = 22;
this.baz[2] = 33;
}
});
但是for循环比$ .each快,所以你可以尝试使用
for(var i=0; i <foo.length; i++)
{
if(foo[i].bar==1)
{
//change the code
}
}
答案 6 :(得分:1)
但是在选择任何一种提及的技术之前,请记住与每种方法相关的性能挑战。
Object iterate For-In, average: ~240 microseconds.
Object iterate Keys For Each, average: ~294 microseconds.
Object iterate Entries For-Of, average: ~535 microseconds.
答案 7 :(得分:1)
您可以使用简单的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);
答案 8 :(得分:0)
您可以利用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);
答案 9 :(得分:0)
您可以玩:
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))
编辑
为避免索引更改:
const markCompleted = (tasks, task) => {
const index = tasks.findIndex(t => t.id === task.id);
tasks[index] = task;
return tasks;
}
答案 10 :(得分:0)
const objArr = [
{prop1: 'value1', prop2: 'value11'},
{prop1: 'value2', prop2: 'value22'},
{prop1: 'value3', prop2: 'option33'},
{prop1: 'value4', prop2: 'option44'}
]
const newObjArr = objArr.map(obj => {
if (['value1', 'value2'].includes(obj.prop1)) {
return {...obj, prop1: 'newValue'}
}
return obj
}
)
// const responseGotten = [
// { prop1: 'newValue', prop2: 'value11' },
// { prop1: 'newValue', prop2: 'value22' },
// { prop1: 'value3', prop2: 'option33' },
// { prop1: 'value4', prop2: 'option44' }
// ]