'How to construct a new array with values of existing objects array in react javascript [duplicate]

lets say i have array

arr = [
    {id: 1 , content: 'content string 1' , ... }
    {id: 2 , content: 'content string 2' , ... }
    {id: 3 , content: 'content string 3' , ... }
    {id: 4 , content: 'content string 4' , ... }
    {id: 5 , content: 'content string 5' , ... }
]

I want to get content string from this array and put this into a new array Like

newArray = ['content string 1', 'content string 2', 'content string 3', 'content string 4', 'content string 5' ]

I hv seen articles on web for the methods to copy values from objects into new array. but not seems to be working.. help would be appreciated



Solution 1:[1]

You can do it with map

const arr = [
    {id: 1 , content: 'content string 1'  },
    {id: 2 , content: 'content string 2'  },
    {id: 3 , content: 'content string 3'  },
    {id: 4 , content: 'content string 4'  },
    {id: 5 , content: 'content string 5'  }
]

const newArr = arr.map((item) => item.content)

console.log(newArr)

Solution 2:[2]

const newArray = arr.map(element => element.content);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Vu
Solution 2 Anastasia