简体   繁体   English

将JavaScript对象/地图转换为嵌套数组?

[英]Converting JavaScript object/map into nested arrays?

I'm trying to use Flot.js to chart some data. 我正在尝试使用Flot.js绘制一些数据。 My JSON response contains this type of data mapping: 我的JSON响应包含这种类型的数据映射:

{123: 5, 534: 0, 724: 3}

I would like to convert that to: 我想将其转换为:

[[123, 5], [534, 0], [724, 3]]

for use with Flot Charts. 用于Flot Charts。 I also need to convert every element to a number instead of a String. 我还需要将每个元素转换为数字而不是字符串。

Is there an existing function to do this? 有现成的功能可以做到这一点吗? Can't for the life of me find it. 我一生无法找到它。

Thanks! 谢谢!

Try this: 尝试这个:

var obj = {123: 5, 534: 0, 724: 3};
var pairs = Object.keys(obj).map(function (key) {
    return [Number(key), Number(obj[key])];
});

console.log(pairs);

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

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