-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.

1

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

This is an often-repeated duplicate question. Please don't post answers to obvious duplicates.
Please vote to close duplicate questions.
1
const newArray = arr.map(element => element.content);

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.