|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wongyusing 于 2018-9-3 17:45 编辑
json数据如下:{"open":{"0":14.99,"1":15,"2":14.91,"3":14.44,"4":13.8,"5":13.67,"6":14.97,"7":13.95,"8":14.04,"9":14.5,"10":13.6,"11":13.5,"12":13.47,"13":14.01,"14":14.01,"15":14.37,"16":14.2,"17":14.14,"18":14.56,"19":14.25},"close":{"0":15.36,"1":14.9,"2":14.73,"3":13.9,"4":13.62,"5":14.35,"6":14.57,"7":14.01,"8":14.04,"9":14.38,"10":13.6,"11":13.52,"12":14.33,"13":14.22,"14":14.37,"15":14.33,"16":14.21,"17":14.58,"18":14.26,"19":13.95}}
我想让这个json数据的格式变成类似下面这种数组形式:[[1,2],[2,5][5,6],[8,10]]
我尝试过用push的方法,然后整个数组都变成了Array()对象了。
这种形式,我在python我知道怎么写,
但在javascript中,我就蒙比了。
明明是数组,怎么就变成了Array()对象了呢??
我的js代码如下for (var key in json['open'],json['close']) {
oc = [json['open'][key],json['close'][key]]
//console.log(oc);
console.log(oc)
candleChart.push([json['open'][key],json['close'][key]]);
};
console.log(candleChart);
1、懂了,目前就是Array对象转换成二维数组[[],[]]
假设你json对象(数组对象也是对象)取值算法没问题(键值对转[]数组),一段例子: var data = {0:'14.99',1:'15'};
// {0: "14.99", 1: "15"}
console.log(data);
var arr = Object.values(data);
// (2) ["14.99", "15"]
console.log(arr);
当然这个是普通数组,不过我个人认为不太需要二维数组。
当然也可以转为二维: var obj = { 0: 14.99, 1: 42 };
// 键数组
var myKeys = Object.values(getObjectKeys(obj)) ;
//值数组
var myValues = Object.values(getObjectValues(obj));
//合并为一个二维数组
var final = [];
for(var i=0;i<myKeys.length;i++){
final.push([myKeys[i],myValues[i]])
}
console.log(final);
// 获得键
function getObjectKeys(object) {
var keys = [];
for(var property in object)
keys.push(property)
return keys
}
//获得值
function getObjectValues(object) {
var values = [];
for(var property in object)
values.push(object[property])
return values
}
|
|