如何在反应中用现有对象数组的值构造一个新数组 javascript
How to construct a new array with values of existing objects array in react javascript
假设我有数组
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' , ... }
]
我想从这个数组中获取内容字符串并将其放入一个新数组中
喜欢
newArray = ['content string 1', 'content string 2', 'content string 3', 'content string 4', 'content string 5' ]
我在网上看过有关将对象的值复制到新数组的方法的文章。但似乎没有用..帮助将不胜感激
你可以用 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)
const newArray = arr.map(element => element.content);
假设我有数组
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' , ... }
]
我想从这个数组中获取内容字符串并将其放入一个新数组中 喜欢
newArray = ['content string 1', 'content string 2', 'content string 3', 'content string 4', 'content string 5' ]
我在网上看过有关将对象的值复制到新数组的方法的文章。但似乎没有用..帮助将不胜感激
你可以用 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)
const newArray = arr.map(element => element.content);