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
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
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())
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
const arr = [{id: 1, Tag_name: "larvel"}, {id: 2, Tag_name: "CSs"},{id:4},{id:5}].map(item => item.id.toString());
The short way