I have an array like this: [1, 2, 3, 4, 5]
and I want to obtain from it an object like this:
[
{ product_id: 1 },
{ product_id: 2 },
{ product_id: 3 },
{ product_id: 4 },
{ product_id: 5 }
]
What is an ES6ish way to obtain this?
I have an array like this: [1, 2, 3, 4, 5]
and I want to obtain from it an object like this:
[
{ product_id: 1 },
{ product_id: 2 },
{ product_id: 3 },
{ product_id: 4 },
{ product_id: 5 }
]
What is an ES6ish way to obtain this?
Problem is in understanding how ES6 .map() return works.
Add round brackets to map, because you want to return that statement. Curly brackets just opens body with no return value. (you have to add it on your own e.g. { return {product_id: item }}
)
const inputArr = [1, 2, 3, 4, 5];
const newArr = inputArr.map(item => ({ product_id: item }));
console.log('arrayLog', newArr);