前端数据格式化{k:v,k:v} => [{k:v},{k:v}]
格式转换
json型数据 转 图表型数据
转换样例:{k:v,k:v} => [{k:v},{k:v}]
// before
json = {
'key1':"value1",
'key2':"value2",
'key3':"value3",
}
// after
array = [
{x:'key1',y:'value1'},
{x:'key2',y:'value2'},
{x:'key3',y:'value3'}
]
如何转换?
//key/value提取 返回结果为key/value组成的数组
let jsonKeys = Object.keys(json);
let jsonValues = Object.values(json);
//遍历jsonkeys
let newArray = jsonKeys.map((v,i) => {
let result = Object.defineProperties({},{
'x':{value:jsonKeys[i],enumerable:true},
'y':{value:jsonValues[i],enumerable:true}
})
return result;
})