Javascript javascript设置对象数组中的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30098942/
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
javascript set all values in array of object
提问by Mischa
Two part question about similar problems. I've got 2 different arrays,
关于类似问题的两部分问题。我有 2 个不同的数组,
array 1:
数组 1:
array1 = [{
name : "users",
checked : true
}, {
name : "active users",
checked : false
}, {
name : "completions",
checked : false
}]
I would like to know if there is an easy way to set all the checked values to true at once. I know this is possible with a for loop:
我想知道是否有一种简单的方法可以一次将所有选中的值设置为 true。我知道这可以通过 for 循环实现:
for (var i = 0 ; i < array.length ; i++) {
array[i].checked = false
}
Even though this is not a lot of code i'm just curious if there is a way to do this without iterating over every object in the array. (not sure if this is even possible or whether this makes any difference whatsoever, so if please correct me if i am wrong about any of this).
尽管这不是很多代码,但我很好奇是否有办法在不迭代数组中的每个对象的情况下做到这一点。(不确定这是否可能,或者这是否有任何区别,所以如果我对此有任何错误,请纠正我)。
array 2:
数组 2:
array2 = [{
value1 : false,
value2 : true,
value3 : false,
value4 : false,
value5 : false,
}]
Is it possible to simply set all the values to true or false at once (again i can use a for loop, but would prefer not to)
是否可以一次简单地将所有值设置为 true 或 false (我可以再次使用 for 循环,但不想这样做)
(i'm using angular and lodash, but haven't been able to find a 'lodash' or 'angular' solution, but any solution will do)
(我正在使用 angular 和 lodash,但一直无法找到“lodash”或“angular”解决方案,但任何解决方案都可以)
回答by Gazler
You can't do this without looping over the elements, however there are functional abstractions you can use instead of for:
如果不循环元素,您就无法做到这一点,但是您可以使用功能抽象来代替for:
For array 1, you can use map
对于数组 1,您可以使用map
array1.map(function(x) {
x.checked = true;
return x
});
Or using _.mapfrom lodash:
或使用_.map从lodash:
_.map(array1, function(x) {
x.checked = true;
return x
});
For array 2 you can use _.mapValuesfrom lodash.
对于数组 2,您可以使用lodash 中的 _.mapValues。
_.mapValues(array2[0], function() {
return true;
});
You can see that lodash implements _.mapusing whileat https://github.com/lodash/lodash/blob/a1b15df6489f47498edda244552c9804e046a45d/lodash.js#L3125
你可以看到,lodash工具_.map使用while在https://github.com/lodash/lodash/blob/a1b15df6489f47498edda244552c9804e046a45d/lodash.js#L3125
回答by Fourat
Even though this is not a lot of code i'm just curious if there is a way to do this without iterating over every object in the array. (not sure if this is even possible or whether this makes any difference whatsoever, so if please correct me if i am wrong about any of this).
Is it possible to simply set all the values to true or false at once (again i can use a for loop, but would prefer not to)
尽管这不是很多代码,但我很好奇是否有办法在不迭代数组中的每个对象的情况下做到这一点。(不确定这是否可能,或者这是否有任何区别,所以如果我对此有任何错误,请纠正我)。
是否可以一次简单地将所有值设置为 true 或 false (我可以再次使用 for 循环,但不想这样做)
No ! You can't visit all townswithout visiting each of them.
There are shortcuts with jQuery like $.eachand lodash like _.mapbut it's just a function that's doing what you would do with the for loop.
不 !如果不参观每个城镇,您就无法访问所有城镇。有类似 jQuery$.each和 lodash 的快捷方式,_.map但它只是一个函数,它正在做你会用 for 循环做的事情。
回答by sumit
You can use es6 fat arrow and make one liner but still it involves iteration .
您可以使用 es6 fat arrow 并制作一个班轮,但它仍然涉及迭代。
array1.map(a=>a.checked=true);
array1 = [{
name : "users",
checked : true
}, {
name : "active users",
checked : false
}, {
name : "completions",
checked : false
}]
array1.map(a=>a.checked=true);
console.log(array1);
回答by eskimomatt
There is no way to set the value of each object in an array without iterating (looping) through the array as you mentioned in your question.
正如您在问题中提到的那样,如果不遍历(循环)数组,就无法设置数组中每个对象的值。

