javascript Lodash:如何向集合中的所有值添加新字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19967761/
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
Lodash: How to add new field to all values in collection
提问by sowdri
Example:
例子:
var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}]
var newArr = _.enhance(arr, { married : false });
console.log(newArr); // [{name: 'a', age: 23, married : false}, {name: 'b', age: 24, married : false}]
I'm looking for something to do this. Note, enhanceis not present in lodash.
Is it possible to do this with lodash?
If not -- possible addition?
我正在寻找可以做到这一点的东西。请注意,提高中不存在lodash。可以用 lodash 做到这一点吗?
如果不是——可能的补充?
Thanks,
谢谢,
回答by Mathletics
You probably want to extend
each
of your objects.
你可能想要extend
each
你的对象。
mu is too short sort of killed my wordplay while making an excellent point. Updated to create an entirely new array.
mu 太短了,在提出一个很好的观点的同时扼杀了我的文字游戏。更新以创建一个全新的数组。
var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}];
var newArr = _.map(arr, function(element) {
return _.extend({}, element, {married: false});
});
If you want to add it to the library,
如果要将其添加到库中,
_.enhance = function(list, source) {
return _.map(list, function(element) { return _.extend({}, element, source); });
}
回答by Janaka Pushpakumara
I use ES6 syntax. I think this will help to u.
我使用 ES6 语法。我认为这对你有帮助。
var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}];
arr.map((element) => {
return element.married = false;
});
console.log(arr); // [{name: 'a', age: 23, married : false}, {name: 'b', age: 24, married : false}]
回答by Maurits Rijk
Using lodashand ES6 arrow notation the solution can become quite short:
使用lodash和 ES6 箭头符号,解决方案可以变得非常简短:
const newArr = _.map(arr, o => _.extend({married: false}, o));
Note: I use the object {married: false}
as the first argument since mutating this object leaves the original array intact. This means however that married
becomes the first field in the resulting object, but this is probably not relevant.
注意:我使用对象{married: false}
作为第一个参数,因为改变这个对象会保持原始数组完好无损。然而,这意味着它married
成为结果对象中的第一个字段,但这可能无关紧要。
回答by mfink
const arr = [{name: 'a', age: 23}, {name: 'b', age: 24}, {name: 'c', age: 25}];
const newArr = arr.map(el => ({ ...el, married: false }));
console.log(newArr);
// [{age: 23, married: false, name: 'a'}, {age: 24, married: false, name: 'b'}, {name: 'c', age: 25, married: false}]
Note: Arrow functions returing an object literal need to wrap the object with parenthesis, e.g., () => ({})
注意:返回对象字面量的箭头函数需要用括号将对象包裹起来,例如, () => ({})
回答by dbriggs
The function lodash has that is closest to what you want is merge: http://lodash.com/docs#merge
lodash 最接近你想要的功能是合并:http://lodash.com/docs#merge
The only slight difference between how merge works and what you want to do is you would need to have an array of equal length to arr all that looks like:
合并的工作方式与您要执行的操作之间唯一的细微差别是您需要一个与 arr 长度相等的数组,如下所示:
var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}];
var marriedArr = [{married : false}, {married : false}];
var newArr = _.merge(arr, marriedArr);
console.log(newArr);
If you attempt to do:
如果您尝试执行以下操作:
var newArr = _.merge(arr, {married : false});
Then merge will work just like concat.
然后合并将像 concat 一样工作。
回答by Vee6
This comes late and it doesn't involve lodash, but I think the cleanest solution if you want the old data mutable is to use a classic for loop iteration. This way you won't load up the memory with a new array.
这来晚了,它不涉及 lodash,但我认为,如果您希望旧数据可变,最干净的解决方案是使用经典的 for 循环迭代。这样你就不会用新数组加载内存。