I have an array of objects that looks like this: π§¨
[
{ apreciated_id: "123g1b1b23kbb3" },
{ apreciated_id: "asd567sad5a7sd" },
{ apreciated_id: "4hk3kjh234kjh4" }
]
But I want it to look like this: π¨βπ¨
["123g1b1b23kbb3", "asd567sad5a7sd", "4hk3kjh234kjh4"]
How can I do it?
Marios
27.5k9 gold badges40 silver badges59 bronze badges
asked Aug 22, 2020 at 0:02
learnbydoing
5192 gold badges7 silver badges16 bronze badges
2 Answers 2
Something like this should do it:
const arr = [
{ apreciated_id: "123g1b1b23kbb3" },
{ apreciated_id: "asd567sad5a7sd" },
{ apreciated_id: "4hk3kjh234kjh4" }
]
const result = arr.map(obj => obj.apreciated_id)
Map takes an array of length n of some type (in this case objects) and trasnforms it according to a function, to an array of length n of some other type (in this case strings)
answered Aug 22, 2020 at 0:04
akaphenom
6,89611 gold badges62 silver badges113 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
learnbydoing
Thank you so much! It works! and now I get it π₯
You can use the map() function to "transform" the array.
let arr = [
{ apreciated_id: "123g1b1b23kbb3" },
{ apreciated_id: "asd567sad5a7sd" },
{ apreciated_id: "4hk3kjh234kjh4" }
];
let res = arr.map(a => a.apreciated_id);
console.log(res);
answered Aug 22, 2020 at 0:08
Rahul Bhobe
4,4714 gold badges19 silver badges35 bronze badges
Comments
lang-js
Array.prototype.map