将带有对象的数组转换为数组

时间:2019-04-26 10:28:00

标签: javascript arrays reactjs object

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

我想拥有一个只有id的数组:

[1, 2, 3]

2 个答案:

答案 0 :(得分: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);

在这里有效

答案 1 :(得分:0)

您可以使用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))