Javascript 将一种数据结构转换为另一种数据结构
嗨,我需要解析一些结构来在折线图中显示一些数据。我有这个数据结构,我需要将其转换为数组示例_输出:Javascript 将一种数据结构转换为另一种数据结构,javascript,Javascript,嗨,我需要解析一些结构来在折线图中显示一些数据。我有这个数据结构,我需要将其转换为数组示例_输出: data = { "el1": [{ "date": "2017.01", "data1": { "series_1": { "a": 10, "b": 20, "c": 50, "d": 15,
data = {
"el1": [{
"date": "2017.01",
"data1": {
"series_1": {
"a": 10,
"b": 20,
"c": 50,
"d": 15,
"e": 8
},
"Series_2": {
"yes": 5,
"no": 3
},
"Series_3": {
"s": 2,
"n": 9
}
},
"text": [{
"t": "header",
"c": "text"
}, {
"t": "header2",
"c": "text2"
}]
}, {
"date": "2017.02",
"data1": {
"series_1": {
"a": 56,
"b": 23,
"c": 45,
"d": 69,
"e": 14
},
"Series_2": {
"yes": 2,
"no": 1
},
"Series_3": {
"s": 6,
"n": 4
}
},
"text": [{
"t": "header",
"c": "text"
}, {
"t": "header2",
"c": "text2"
}]
}, {
"date": "2017.03",
"data1": {
"series_1": {
"a": 15,
"b": 12,
"c": 10,
"d": 54,
"e": 4
},
"Series_2": {
"yes": 20,
"no": 16
},
"Series_3": {
"s": 9,
"n": 7
}
},
"text": [{
"t": "header",
"c": "text"
}, {
"t": "header2",
"c": "text2"
}]
}
]
};
我需要像chartist.js这样的输出
var example_output = [{
labels: ['2017.01', '2017.02', '2017.03'],
series: {
[10, 56, 15],
[20, 23, 12],
[50, 45, 10],
[15, 69, 54],
[8, 14, 4]
},
labels: ['2017.01', '2017.02', '2017.03'],
series: {
[5, 2, 20],
[3, 1, 16]
},
labels: ['2017.01', '2017.02', '2017.03'],
series: {
[2, 6, 9],
[9, 4, 7]
},}] ;
请比较原始输出和示例输出中的数字,以更好地了解其外观。我使用代码来解析,但出现了一些问题:
function parseData(data) {
var _newData = {};
var allSeries = [];
data.elements.forEach(function(el){
_newData[el.date] = el.info
if(allSeries.length==0)
allSeries = Object.keys(el.info);
});
return allSeries.map(function(el) {
var obj = {
labels: [],
series: []
};
obj.labels = Object.keys(_newData);
Object.keys(_newData).forEach(function(_el) {
obj.series.push(Object.values(_newData[_el][el]));
});
var _newSeries = [];
obj.series[0].forEach(function(el, i){
_newSeries.push([el, obj.series[1][i]]);
});
obj.series = _newSeries;
return obj;
});
}
您可以使用array#forEach
和array#reduce
。使用第一个循环循环遍历el1
,然后对series
键使用reduce,并将结果累积到输出\u示例中
注意:输出中的系列
不正确。它应该是数组而不是对象
[代码>宏量数据={“el1”号““,,,,,,,,,,,,,,,,,,,,,,,:c“:50,”d“:10,”b“:20,”c“:20,”c“:50,”c“:50,”d“:15,”e“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,":1},“第三组”:{“s”:6,“n”:4},“文本”:[{“t”:“标题”,“c”:“文本”},{“t”:“标题2”,“c”:“文本2”},{“日期”:“2017.03”,“数据1”:{“系列1”:{“a”:15,“b”:12,“c”:10,“d”:54,“e”:4},“系列2”:{“是”:20,“否”:16},“系列3”:{“s”:9,“n”:7},“文本”{“标题”,“文本”{“t”:“标题”,“文本2”},{;
转置=(arr)=>arr.reduce((r,v,i)=>{
(r[i]=r[i]| |[])。推(v);
返回r;
},[]);
例如,输出=[];
data.el1.forEach((o)=>{
Object.keys(o.data1).reduce((r,k,i)=>{
r[i]=r[i]|{};
if(r[i]中的“标签”)
r[i]['labels'].推送(o.date);
其他的
r[i]['labels']=[o.date];
if('r[i]中的“系列”)
Object.values(o.data1[k]).forEach((v,j)=>r[i]['series'][j].push(v));
其他的
r[i]['series']=转置(对象值(o.data1[k]);
返回r;
},例如输出);
},[]);
console.log(示例_输出)代码>
作为控制台包装{max height:100%!important;top:0;}
对不起,我的意思是var数据是我的JSONNo。不要混淆JavaScript和JSON。JSON是JavaScript对象的字符串表示形式。代码中没有任何对象以字符串格式表示。因此,您的代码不包含JSON。好的,谢谢您的建议,谢谢您的帮助!你的剧本写得很好!。但我有一个问题,这个脚本是用ECMA 6编写的,在IOS中无法使用函数替换箭头函数(=>
)