修改对象数组中的对象属性

时间:2022-09-25 09:14:34
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);

http://jsfiddle.net/98EsQ/

http://jsfiddle.net/98EsQ/

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]}]

5 个解决方案

#1


18  

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;
    }
});

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 you have other looping options in this other SO answer.

...或者你在其他SO答案中有其他循环选项。

#2


8  

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];
    }
}

#3


2  

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;
   }
 })

#4


1  

$.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
}
}

#5


0  

You can use find and change its property.

您可以使用查找并更改其属性。

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);

#1


18  

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;
    }
});

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 you have other looping options in this other SO answer.

...或者你在其他SO答案中有其他循环选项。

#2


8  

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];
    }
}

#3


2  

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;
   }
 })

#4


1  

$.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
}
}

#5


0  

You can use find and change its property.

您可以使用查找并更改其属性。

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);