Javascript 如何设置javascript对象数组中所有对象的特定属性值(lodash)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42286442/
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 set specific property value of all objects in a javascript object array (lodash)
提问by sith
I have following object array:
我有以下对象数组:
var arr = [
{
id : "a1",
guid : "sdfsfd",
...
value : "abc",
status: "active"
},
{
id : "a2",
guid : "sdfsfd",
...
value : "def",
status: "inactive"
},
{
id : "a2",
guid : "sdfsfd",
...
value : "def"
},
...
]
How to set "status" property of each object to "active". So the resulting array will be:
如何将每个对象的“状态”属性设置为“活动”。所以结果数组将是:
var arr = [
{
id : "a1",
guid : "sdfsfd",
...
value : "abc",
status: "active"
},
{
id : "a2",
guid : "sdfsfd",
...
value : "def",
status: "active"
},
{
id : "a2",
guid : "sdfsfd",
...
value : "def",
status: "active"
},
...
]
Additionally this should create the property "active" if doesn't exists.
此外,如果不存在,这应该创建属性“active”。
I can do this using for loops. But I'm pretty much sure lodashcan do this in one line like:
我可以使用 for 循环来做到这一点。但我非常确定lodash可以在一行中做到这一点,例如:
arr = _.set_property(arr, "status", "active");
采纳答案by random_user_name
Indeed, you don't needLodash, but the question is tagged Lodash, and using Lodash offers some useful defenses that reduces the risk of errors. This solution utilizes _.forEachand _.set
实际上,您不需要Lodash,但问题被标记为 Lodash,并且使用 Lodash 提供了一些有用的防御措施,可以降低出错的风险。此解决方案使用_.forEach和_.set
// _.forEach won't throw errors if arr is not an array...
_.forEach(arr, function (obj) {
// _.set won't throw errors if obj is not an object. With more complex objects, if a portion of the path doesn't exist, _.set creates it
_.set(obj, 'status', 'active');
});
If you wanted to make it abstract, you could build a Lodash mixin:
如果你想让它抽象,你可以构建一个 Lodash mixin:
_.mixin({
setProperty: function(arr, key, val) {
_.forEach(arr, function (obj) {
_.set(obj, path, val);
});
}
});
Then, you could use it exactly as you described:
然后,您可以完全按照您的描述使用它:
_.setProperty( arr, 'status', 'active' );
回答by Legends
You don't need lodash for this.
你不需要 lodash 这个。
The first object is missing your status property and it will be added.
第一个对象缺少您的状态属性,它将被添加。
var arr = [
{
id : "a1",
guid : "sdfsfd",
value : "abc"
},
{
id : "a2",
guid : "sdfsfd",
value : "def",
status: "inactive"
},
{
id : "a2",
guid : "sdfsfd",
value : "def",
status: "active"
}
];
// SHOWING THREE WAYS HOW YOU CAN DO IT
// MUTABLE VERSIONS - We change the original array
arr.forEach((el)=>{el.status = "active";}) // ES6
// or
arr.forEach(function(el){el.status = "active";})
//or
// IMMUTABLE VERSION - We create a new array using `map`
const arrImmutableVersion = arr.map(e => ({...e, status: "active"})); // ES6
//--------------------------------------------------------------
// RESULTS:
console.log("logging results of object 'arr'");
console.log(arr);
console.log("---------------------------------------------------------");
console.log("logging results of object 'arrImmutableVersion'");
console.log(arrImmutableVersion);

