-2

let's say I have an 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've seen many articles on web for the methods to copy values from objects into new array, but not seems to be working.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
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
  • 14,512
  • 4
  • 21
  • 31
1
const newArray = arr.map(element => element.content);
Anastasia
  • 712
  • 1
  • 5
  • 11
  • 2
    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