i am trying to parse json
request(requestOptions,(err,res,body)=>{
for(var i in body){
console.log(body.data[0].quote.USD.price)
}
})
and the json is something like
{ status: {something},
data: [{
id:1,
name:'Bitcoin',
symbol:'BTC',
slug:'bitcoin',
circulating_supply:17400325,
total_supply:17400325,
max_supply:21000000,
date_added:'2013-04-28T00:00:00.000Z',
num_market_pairs:6618,
tags:[
Array
],
cmc_rank:1,
last_updated:'2018-11-30T11:00:28.000Z',
quote:[
Object
]
}]
}
quote object is :
{ USD:
{ price: 4071.55349237,
volume_24h: 5966638087.9657,
percent_change_1h: -3.18515,
percent_change_24h: -6.66359,
percent_change_7d: -5.74611,
market_cap: 70846354022.12302,
last_updated: '2018-11-30T11:03:22.000Z' } }
"there is two json object one i mentioned and other one is duplicate of above json".
{ status:{ },
data:[ ]
}
i run above code it gives me this output:
1 Bitcoin BTC 4030.51947765 1 Bitcoin BTC 4030.51947765
kindly help me how i can get all "id","name", " symbol", "price" of object, and save it to one array.
Simply map it:
https://stackblitz.com/edit/js-c1fwsf
const req = {
data: [
{
id:1,
name:'Bitcoin',
symbol:'BTC',
slug:'bitcoin',
circulating_supply:17400325,
total_supply:17400325,
max_supply:21000000,
date_added:'2013-04-28T00:00:00.000Z',
num_market_pairs:6618,
cmc_rank:1,
last_updated:'2018-11-30T11:00:28.000Z',
quote:[
{
USD:
{
price: 4071.55349237,
volume_24h: 5966638087.9657,
percent_change_1h: -3.18515,
percent_change_24h: -6.66359,
percent_change_7d: -5.74611,
market_cap: 70846354022.12302,
last_updated: '2018-11-30T11:03:22.000Z'
}
}
],
},
{
id:2,
name:'Litecoin',
symbol:'LTC',
slug:'litecoin',
circulating_supply:17400325,
total_supply:17400325,
max_supply:21000000,
date_added:'2013-04-28T00:00:00.000Z',
num_market_pairs:6618,
cmc_rank:1,
last_updated:'2018-11-30T11:00:28.000Z',
quote:[
{
USD:
{
price: 221.55349237,
volume_24h: 5966638087.9657,
percent_change_1h: -3.18515,
percent_change_24h: -6.66359,
percent_change_7d: -5.74611,
market_cap: 70846354022.12302,
last_updated: '2018-11-30T11:03:22.000Z'
}
}
]
}]
}
const res = req.data.map(x => ({
name: x.name,
id: x.id,
symbol: x.symbol,
price: x.quote[0].USD.price,
}))
console.log(res)
Try this one
let mydata = { status: "something",
data: [{
id:1,
name:'Bitcoin',
symbol:'BTC',
slug:'bitcoin',
circulating_supply:17400325,
total_supply:17400325,
max_supply:21000000,
date_added:'2013-04-28T00:00:00.000Z',
num_market_pairs:6618,
tags:[
"Something"
],
cmc_rank:1,
last_updated:'2018-11-30T11:00:28.000Z',
quote:
{ USD:
{ price: 4071.55349237,
volume_24h: 5966638087.9657,
percent_change_1h: -3.18515,
percent_change_24h: -6.66359,
percent_change_7d: -5.74611,
market_cap: 70846354022.12302,
last_updated: '2018-11-30T11:03:22.000Z' } }
}]
}
let myArray = []
mydata.data.map((o)=>{
const { id, name, symbol } = o
myArray.push({
id,
name,
symbol,
price : o.quote.USD.price
})
})
console.log(myArray)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.