-2

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

noobie
  • 31
  • 6

2 Answers2

2

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)
Nick Vu
  • 8,514
  • 1
  • 14
  • 25
1
const newArray = arr.map(element => element.content);
Anastasia
  • 562
  • 1
  • 3
  • 10
  • 1
    This is an **often-repeated** duplicate question. Please don't post answers to obvious duplicates. Also, code-only answers aren't useful answers. It's important to explain what you did. – T.J. Crowder May 16 '22 at 09:45