我有以下图表数组,其中包含foo,bar和baz对象,每个对象都包含一个带有数据点的values数组。
问题是,从我的API端点,我得到了可变数量的数据点 我需要规范化我的对象,以便:
数据点需要按标签acending排序(例如a,b,c ..)
如果数据点不存在,则在特定对象中存在, 在任何其他数据点数组中,它生成的值为零
最后,每个对象中的数据点数必须相同。
以下是代码示例:
var r = function() { return Math.random() * 10; };
var chart = [
{key:'foo', values: [['a', r()], ['b', r()], ['c', r()], ['d', r()]]},
{key:'bar', values: [['b', r()], ['c', r()], ['d', r()], ['e', r()]]},
{key:'baz', values: [['c', r()], ['d', r()], ['e', r()], ['f', r()]]}
];
// desired output, where x is the random value returned by r()
// any values that are unavailable must be 0
chart = [
{key:'foo', values: [['a', x], ['b', x], ['c', x], ['d', x], ['e', 0], ['f', 0]]},
{key:'bar', values: [['a', 0], ['b', x], ['c', x], ['d', x], ['e', x], ['f', 0]]},
{key:'baz', values: [['a', 0], ['b', 0], ['c', x], ['d', x], ['e', x], ['f', x]]}
];
答案 0 :(得分:1)
您的字母数字对可能更好地用对象而不是数组表示,但我相信这解决了所述的问题。
var datapointLabels = {}; // Create object to record which labels exist.
for(var i = 0; i < chart.length; i++) { // Iterate through `chart` items.
for(var j = 0; j < chart[i].values.length; j++) { // Iterate through `values` items.
var datapointLabel = chart[i].values[j][0];
datapointLabels[datapointLabel] = true;
}
}
// Skip nested loops above if datapoint labels are pre-known; construct `datapointsLabels` object with one loop, or manually without looping, instead.
var newChart = []; // Must create a new array to hold the padded data (will need to refer to old array throughout process).
for(var i = 0; i < chart.length; i++) {
newChart[i] = {};
newChart[i].key = chart[i].key;
newChart[i].values = [];
for(var datapointLabel in datapointLabels) { // Iterate through all datapoint labels in our records object.
var datapoint = 0; // Set default datapoint as zero.
for(var j = 0; j < chart[i].values.length; j++) { // Look at each `value` pair to see if it matches the current datapoint label.
if(chart[i].values[j][0] === datapointLabel) {
datapoint = chart[i].values[j][1]; // Overwrite default if found.
j = chart[i].values.length; // Skip further checks (optional, minor efficiency increase).
}
}
newChart[i].values.push([datapointLabel, datapoint]); // Push found or default data to new array.
}
}
如果 能够重新定义您的数据结构,可以使用对象键查找来切割for
- 循环:
var chart = [
{ key: 'foo', values: { a: r(), b: r(), c: r(), d: r() } },
{ key: 'bar', values: { b: r(), c: r(), d: r(), e: r() } },
{ key: 'baz', values: { c: r(), d: r(), e: r(), f: r() } }
];
var datapointLabels = {}; // Create object to record which labels exist.
for(var i = 0; i < chart.length; i++) { // Iterate through `chart` items.
for(var datapointLabel in chart[i].values){ // Iterate through `values` items.
datapointLabels[datapointLabel] = true;
}
}
// Skip nested loops above if datapoint labels are pre-known; construct `datapointsLabels` object with one loop, or manually without looping, instead.
var newChart = []; // Must create a new array to hold the padded data (will need to refer to old array throughout process).
for(var i = 0; i < chart.length; i++) {
newChart[i] = {};
newChart[i].key = chart[i].key;
newChart[i].values = {};
for(var datapointLabel in datapointLabels) { // Iterate through all datapoint labels in records object.
var datapoint = chart[i].values[datapointLabel] || 0;
newChart[i].values[datapointLabel] = datapoint;
}
}
答案 1 :(得分:1)
我的解决方案使用Array.map()
。
var temp = ["a","b","c","d","e","f"];
var newchart = chart.map(function (obj) {
var objValKeys = obj.values.map(function (objVal) {
return objVal[0];
});
return {
key: obj.key,
values: temp.map(function (thisval) {
var index = objValKeys.indexOf(thisval);
var actlVal = (index+1) ? obj.values[index][1] : 0;
return [thisval, actlVal];
})
};
});
上查看