Reputation: 10583
I have this array of objects
[ { id: '573267d06b2957ab24c54d59' },
{ id: '573267d06b2957ab24c54d5a' },
{ id: '573267d06b2957ab24c54d5b' },
{ id: '573267d06b2957ab24c54d5c' },
{ id: '573267d06b2957ab24c54d5d' }
]
I wish to convert it to the following in NodeJs
[ '573267d06b2957ab24c54d59',
'573267d06b2957ab24c54d5a',
'573267d06b2957ab24c54d5b',
'573267d06b2957ab24c54d5c',
'573267d06b2957ab24c54d5d'
]
It seems like it should be easy given the right library/package, but I am struggling to find the right wording to "flatten" the array into the IDs of the contained objects.
Upvotes: 5
Views: 5519
Reputation: 8065
Say your array of objects is called arr
, just do this:
var arrayOfStrings = arr.map(function(obj) {
return obj.id;
});
map
will iterate over the array and create a new array based on how you define your function. In this case we return the value of the id
key in each case to build out the desired array of ids.
Upvotes: 7