Javascript 映射函数:向对象添加新的键和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49273759/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:30:36  来源:igfitidea点击:

Javascript map function :add new key and value to object

javascriptarrays

提问by iam batman

I have JSON array like this

我有这样的 JSON 数组

var array= [{id:1,name:'foo'},{id:2,name:'bar'}]

I would like to add a new key (eg:isApproved) to each object in the existing array

我想isApproved为现有数组中的每个对象添加一个新键(例如:)

expected output:

预期输出:

var array= [{id:1,name:'foo',isApproved:true},{id:2,name:'bar',isApproved:true}] 

I used the map function to achieve this

我使用地图功能来实现这一点

array.map(function(e,index){
     e.isApproved[index]= true
}

But this not worked for me

但这对我不起作用

回答by Cata John

You were really close. You do not need the index here. The map passes through every element of the array, so 'e' will be each object in your array.

你真的很亲近。您不需要此处的索引。映射通过数组的每个元素,因此 'e' 将是数组中的每个对象。

var array= [{id:1,name:'foo'},{id:2,name:'bar'}];

array.map(function(e){
     e.isApproved = true;
});
          
console.log(array);

回答by Kejt

With this code, you wont mutate objects inside your array

使用此代码,您不会改变数组内的对象

const arr = [{id:1,name:'foo'},{id:2,name:'bar'}];
const mapped = arr.map(element => Object.assign(element, {isApproved: true})

More new approach would be using spread operator:

更多的新方法是使用扩展运算符:

const arr = [{id:1,name:'foo'},{id:2,name:'bar'}];
const mapped = arr.map(element => ({isApproved: true ,...element}))

Snippet

片段

const arr = [{id:1,name:'foo'},{id:2,name:'bar'}];
    const mapped = arr.map(element => ({isApproved: true ,...element}))


console.log(mapped)

回答by David Ibl

Try this, you don't need the index:

试试这个,你不需要索引:

var array= [{id:1,name:'foo'},{id:2,name:'bar'}];

array.map(value => value.isApproved = true);
console.log(array)

回答by gurvinder372

e[index]doesn't make sense since this indexis meant for the arrayyou are iterating.

e[index]没有意义,因为这index是针对array您正在迭代的。

Set the property directly to e

将属性直接设置为 e

array.map(function(e,index){
     e.isApproved = true; 
}

回答by Muhammad Danial Iqbal

Use the actual item 'e' in the map

使用地图中的实际项目“e”

Map also gives you the facility to alter each element and return them in a new array. This can be useful if you do not want your current array to alter its state rather you need a modified form of the same array.

Map 还为您提供了更改每个元素并将它们以新数组形式返回的便利。如果您不希望当前数组改变其状态,而是需要相同数组的修改形式,这会很有用。

check this code:

检查此代码:

var array= [{id:1,name:'foo'},{id:2,name:'bar'}];

var modifiedArray = array.map(function(e,index){
    return Object.assign({isApproved:true},e);
});

console.log(array);
console.log(modifiedArray);

Output:

输出:

//array
[{id: 1, name: "foo"},
{id: 2, name: "bar"}]

//modifiedArray
[{isApproved: true, id: 1, name: "foo"},
{isApproved: true, id: 2, name: "bar"}]

回答by Ininiv

<script>

var array= [{id:1,name:'foo'},{id:2,name:'bar'}]
array.forEach(function(e,index){
     e.isApproved= true;
})
console.log(array);

</script>

You can use forEach instead of map.

您可以使用 forEach 而不是 map。