简体   繁体   中英

Javascript: Adding a property to an array of objects

I have an array of objects as follows:

var myarray=[{"name":"John","address":"home"},{"name":"Peter","address":"home"}]

and I would like to run a function to add a property to the array as follows:

[{"name":"John","address":"home","collection":"friend"},
 {"name":"Peter","address":"home","collection":"friend"}]

I have tried doing this:

myarray=myarray.map(function (err, myarray){
    myarray.collection="friend";
    return myarray;
}
console.log(myarray)

But the console continues to return this:

[{0},{1}] 

Can anyone help me? Thank you

Your code is not adding the property to the contents of the array. The values of the array are given as the first parameter to the callback function (the second parameter is an index, and not the array itself—that's the third parameter). Simply assign the new property to the first parameter of the callback function, rather than the second one.

Edit - As @zerkms points out, however, if you're looking to update the current array rather than generate a new array, map is probably not best solution here. forEach provides a method for iterating over the current array, and modifying each of its values (which is what you're doing). This would looks omething like this:

myarray.forEach(function(value) {
    value.collection = "friend";
});

As you'll notice in the documentation for .map , the callback function returns the new value that will appear in the new array that is generated by map ; if you're changing the current array in place (ie by modifying the properties of its contents), there's no need to return anything.

myarray.map(function(value) {
    value.collection = "friend";
});

Also note that both map and forEach are methods, so you need to close the method invocation with ) .

Wrong use of map(). The first argument of map() is the current element of the array, the second argument is it's index.
For example:

['a','b','c'].map(function(element, index){console.log(element, index)});

Will result in

a 1
b 2
c 3

So inside your function myarray was your index, and you were trying to add the property to the index.

Now you have to options. Either you use the map() as it's ment to be used and assign it's return value to myarray :

myarray = myarray.map(function(element) {
    element.collection = "friend";
    return element;
});

or you can, because objects are not getting copied but referenced when passed as an argument, not care about the return values and modify the elements directly:

myarray.map(function(element) {
    element.collection = "friend";
});   // returns [undefined, undefined ...]

This, however, isn't the way one should use map()

Better: Use forEach()

myarray.forEach(function(element) {
    element.collection = "friend";
}); 

Hope it helped. Greets!

All you have to do is changing the reference object within map function

myarray.map(function (value){
  value.collection = "friend";
});

console.log(myarray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM