I have an object list like this, I am getting this response from one of the api:
{
"1/22/20": {
"new_daily_deaths": 0,
"total_cases": 1,
},
"1/23/20": {
"new_deaths": 0,
"total_cases": 10,
},
"1/24/20": {
"new_deaths": 0,
"total_cases": 20
}
}
Expected Output:
{
x:1/22/20,1/23/20,1/24/20 // key of every object
y:1,10,20 //total_cases
}
Please help me how can we achieve this. I tried object.stringify
but its not giving me an expected output.
You can use Object.keys()
& Object.entries()
methods for this like:
const data = { "1/22/20": { "new_daily_deaths": 0, "total_cases": 1, }, "1/23/20": { "new_deaths": 0, "total_cases": 10, }, "1/24/20": { "new_deaths": 0, "total_cases": 20 } } const keys = Object.keys(data).join(',') const cases = Object.entries(data).map(([k,v]) => v).map(x=>x.total_cases).join(',') const result = { x: keys, y: cases} console.log(result)
output as {x:1/22/20, y:1}, {x:1/23/20,y:10}
const data = { "1/22/20": { "new_daily_deaths": 0, "total_cases": 1, }, "1/23/20": { "new_deaths": 0, "total_cases": 10, }, "1/24/20": { "new_deaths": 0, "total_cases": 20 } } const result = Object.entries(data).map(([k,v]) => ({x: k, y: v.total_cases})) console.log(result)
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.