简体   繁体   English

将具有多个数组的对象转换为多维数组

[英]Converting an object with multiple arrays to a multi dimensional array

I have this format:我有这种格式:

Data = {
    "count": [ 62, 58, 10, 6 ],
    "categoires": [ "a1", "a2", "a3", "a4" ]
}

I need to get get data in to the following format:我需要按以下格式获取数据:

data = [
    [ "a1", 62 ],
    [ "a2", 58 ],
    [ "a3", 10 ],
    [ "a4", 6 ]
]

Can anyone help me do this?谁能帮我做到这一点?

You can loop using for and length properties:您可以使用forlength属性进行循环:

var data = [];
for (var i = 0; i < Data.count.length; i++)
    data[i] = [ Data.categoires[i], Data.count[i] ];

Try using while loop尝试使用while循环

 var data = { "count": [62, 58, 10, 6], "categoires": ["a1", "a2", "a3", "a4"] } var res = [], // array to push values to i = 0, // begin at index `0` len = data["categoires"].length; // max length while (i < len) { res.push([data["categoires"][i], data["count"][i]]); // push data tob `res` ++i; // increment index } console.log(res); document.body.textContent = JSON.stringify(res, null, 2)
 body { white-space:pre; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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