0

I pull some data from an API which returns something like this

[{"id":21},{"id":22},{"id":24}]

I will have to post the data received to a database via a different API, but when I'm sending I want it to be sent like [21,22,24] For example the variable that receives the data from the API is called valuesToSend which comes [{"id":21},{"id":22},{"id":24}] but when posting, it should be [21,22,24]

postToDb(){
alert(this.valuesToSend)
}
user6579134
  • 749
  • 3
  • 10
  • 35
  • Read about [`Array.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). – axiac May 08 '19 at 13:11
  • 1
    Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – adiga May 08 '19 at 13:12
  • `this.valuesToSend.map(a => a.id)` – adiga May 08 '19 at 13:13

1 Answers1

0

If you know the object key, using ES6, Array.map() and object destructuring, you could do it like this

const a = [{ "id": 21 }, { "id": 22 }, { "id": 24 }];
const valuesToSend = a.map(({ id }) => id);
alert(valuesToSend)
holmrenser
  • 425
  • 3
  • 11