-2

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
  • 26,333
  • 8
  • 32
  • 52
learnbydoing
  • 309
  • 1
  • 5
  • 14

2 Answers2

0

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)

akaphenom
  • 6,728
  • 10
  • 59
  • 109
0

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);
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32