Skip to content

Latest commit

 

History

History
25 lines (16 loc) · 645 Bytes

map-array-into-array-of-objects.md

File metadata and controls

25 lines (16 loc) · 645 Bytes

If you want to convert an array of say, number or string, into an array of objects, so these number or string will be used as key values, you can try below method:

const fruitArray = ['apple', 'pear', 'banana', 'peach', 'waterlemon']

console.log(fruitArray.map(f => ({ fruitName: f, price: 10 }))) 

The result would be

[
    {fruitName: "apple", price: 10},
    {fruitName: "pear", price: 10},
    {fruitName: "banana", price: 10},
    {fruitName: "peach", price: 10},
    {fruitName: "waterlemon", price: 10}
]

Don't forget to use parentheses to wrap around the object, otherwise it won't work.