-5
{"result":[{"id":1,"currency":"USD"},{"id":2,"currency":"PLN"},{"id":3,"currency":"EUR"}],"success":true}

I would like to have array with only id:

[1, 2, 3]
  • 6
    Have you tried anything? There's a tonne of questions like this already on Stack Overflow. With minimal research, at least one of them will have the information you're looking for. – Lewis Apr 26 '19 at 10:29
  • 3
    Welcome to stackoverflow. Please have a look at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and edit your question accordingly. You should show us what you have tried so far and what you want to achieve. – Björn Böing Apr 26 '19 at 10:30
  • `result` is an array, so you'll want to start with understanding how to loop over that. MDN is [always a good place to start with understanding JS](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration). – Andy Apr 26 '19 at 10:32
  • 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 Apr 26 '19 at 10:34
  • `obj.result.map(a => a.id)` – adiga Apr 26 '19 at 10:35
  • In easier way, for proper understanding ```let obj = {"result":[{"id":1,"currency":"USD"},{"id":2,"currency":"PLN"},{"id":3,"currency":"EUR"}],"success":true} let arr = [] obj.result.forEach(element => { arr.push(element.id) }); console.log(arr)``` – Chandan Kumar Apr 26 '19 at 10:36

2 Answers2

0
let data = {result":[{"id":1,"currency":"USD"},{"id":2,"currency":"PLN"},{"id":3,"currency":"EUR"}],"success":true};

let ids = data.result.map(item => item.id);

Here it works

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
H.K
  • 1
  • 1
0

You could do with array#map

var arr = {"result":[{"id":1,"currency":"USD"},{"id":2,"currency":"PLN"},{"id":3,"currency":"EUR"}],"success":true};

console.log(arr['result'].map(a=>a.id))
prasanth
  • 22,145
  • 4
  • 29
  • 53