Sunday, March 5, 2023

using .map() to iterate and modify objects in array

 const myUsers = [

    { name: 'shark', likes: 'ocean' },
    { name: 'turtle', likes: 'pond' },
    { name: 'otter', likes: 'fish biscuits' }
]

const usersByLikes = myUsers.map(item => {
    const container = {};

    container[item.name] = item.likes;
    container.age = item.name.length * 10;

    return container;
})

console.log(usersByLikes);

Source: https://www.digitalocean.com/community/tutorials/4-uses-of-javascripts-arraymap-you-should-know 

No comments:

Post a Comment