简体   繁体   中英

PUSH JSON items to array

I have this JSON string

{"Task": [Hours per Day],"Work": [11],"Eat": [6],"Commute": [4],"Sleep": [3]}

I want to push it's items to a jQuery array.

I already tried JSON.parse .

Normally I can push parameters like this:

MyArr.push(['Task','Hours per Day']);
MyArr.push(['Work','11']);
MyArr.push(['Eat','6']);

and so on.

How can I do the same with the JSON string?

Can you not just parse the JSON into an object and loop through?

 var json = '{"Task": ["Hours per Day"],"Work": [11],"Eat": [6],"Commute": [4],"Sleep": [3]}' var obj = JSON.parse(json); MyArr = [] for (var key in obj) { MyArr.push([key, obj[key][0]]) } console.log(MyArr) 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM