1

Convert the array of object into array in react

[{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}]

I want this output

["1","2","4","5"]

Plz help me

1

4 Answers 4

2

You can use Array.prototype.map

[{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}].map((e) => e.id.toString())
Sign up to request clarification or add additional context in comments.

Comments

2
const arr = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}]

let newArr = []

arr.forEach((item)=> {

 newArr.push(item.id.toString())

})

Comments

2

You can use Array.prototype.map():

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

const array = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}];
const result = array.map(item => item.id.toString());

console.log(result);
// prints ["1","2","4","5"] to the console

Comments

0
const arr = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}].map(item => item.id.toString());

The short way

1 Comment

this exact answer has already been submitted twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.