Reputation: 11
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
Upvotes: 1
Views: 74
Reputation: 979
const arr = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}].map(item => item.id.toString());
The short way
Upvotes: 0
Reputation: 16309
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
Upvotes: 2
Reputation: 246
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())
})
Upvotes: 2
Reputation: 100
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())
Upvotes: 2