以下代码循环遍历两个数组,并在运行后输出以下内容。
GraphCategories = [
"AdminFee",
"AdminFeeVat",
"CaptureCharge",
"CaptureChargeVat",
"CardReplacementFee",
"CardReplacementFeeVat",
"DriverBillAllowFee",
"DriverBillAllowFeeVat",
"FleetSaverFee",
"FleetSaverFeeVat",
"FuncdingInt",
"LostCardProtectionFee",
"LostCardProtectionFeeVat",
"Other",
"OtherVat",
"TransactionCharge",
"TransactionChargeVat",
"TurnkeyFee",
"TurnkeyFeeVat",
"WalletReplacementFee",
"WalletReplacementFeeVat"
];
seriesData =
[
{name: "AdminFee", group: "2019-10", y: 262038},
{name: "AdminFee", group: "2019-11", y: 262038},
{name: "AdminFeeVat", group: "2019-10", y: 39341.4},
{name: "AdminFeeVat", group: "2019-11", y: 39341.4},
{name: "CaptureCharge", group: "2019-10", y: 0},
{name: "CaptureCharge", group: "2019-11", y: 0},
{name: "CaptureChargeVat", group: "2019-10", y: 0}
];
function BuildStacked(seriesData, GraphCategories) {
var newGraphdata = [];
seriesData.forEach(function (SeriesVal, SeiresKey) {
let previousVal = false;
GraphCategories.forEach(function (GraphVal, GraphKey) {
if (SeriesVal.name === GraphVal) {
if (previousVal === false) {
console.log(previousVal);
console.log(GraphVal);
if (previousVal !== GraphVal) {
newGraphdata.push({ 'name': SeriesVal.name, 'data': [SeriesVal.y], 'stack': SeriesVal.group });
previousVal = SeriesVal.name;
}
}
};
});
});
console.log(newGraphdata);
}
这是代码生成的最终数组
Array
0: {name: "AdminFee", data: Array(1), stack: "2019-10"}
1: {name: "AdminFee", data: Array(1), stack: "2019-11"}
2: {name: "AdminFeeVat", data: Array(1), stack: "2019-10"}
3: {name: "AdminFeeVat", data: Array(1), stack: "2019-11"}
4: {name: "CaptureCharge", data: Array(1), stack: "2019-10"}
5: {name: "CaptureCharge", data: Array(1), stack: "2019-11"}
6: {name: "CaptureChargeVat", data: Array(1), stack: "2019-10"}
7: {name: "CaptureChargeVat", data: Array(1), stack: "2019-11"}
最终结果应如下。
其中键3和4的值合并为键1和2的位置。 因为名称包含一个已经插入的项目
0: {name: "AdminFee", data: Array(1), stack: "2019-10"}
1: {name: "AdminFee", data: Array(1), stack: "2019-11"}
2: {name: "AdminFeeVat", data: Array(1), stack: "2019-10"}
3: {name: "AdminFeeVat", data: Array(1), stack: "2019-11"}
||
||
\/
0: {name: "AdminFee", data: Array(2), stack: "2019-10"}
1: {name: "AdminFee", data: Array(2), stack: "2019-11"}
所以我尝试实现的输出或多或少是以下格式
{
name: 'AdminFee',
data: [AdminFee.data, AdminFeeVat.data],
stack: '2019-10'
}, {
name: 'AdminFee',
data: [AdminFee.data, AdminFeeVat.data],
stack: '2019-11'
}
连接应该在以下情况下发生在数据中 AdminFee,AdminFeeVat具有相同的堆栈/日期
因此,结果应该是
0: {name: "AdminFee", data: Array(2), stack: "2019-10"}
1: {name: "AdminFee", data: Array(2), stack: "2019-11"}
答案 0 :(得分:0)
您需要使用Object并为结果的每一行定义一个键,而不是使用array.push()
,最后,如果需要数组作为输出,请将其转换为数组。
例如:
newGraphdata.push({ 'name': SeriesVal.name, 'data': [SeriesVal.y], 'stack': SeriesVal.group });
将转换为:
if (typeof newGraphdata.definedKey == 'undefined') {
newGraphdata.definedKey ={ 'name': SeriesVal.name, 'data': [SeriesVal.y], 'stack': SeriesVal.group };
}else{
newGraphdata.definedKey.data.push(SeriesVal.y);
}
,最后您可以将结果转换为数组,如下所示: How to convert an Object {} to an Array [] of key-value pairs in JavaScript
如果您也想使用适当的数据类型,也可以在这里看到: https://medium.com/front-end-weekly/es6-map-vs-object-what-and-when-b80621932373
答案 1 :(得分:0)
此修复程序有点脏,但是可以使用, 由于现在我或多或少都以所需的格式获取了数据,因此Highcharts尚未将数据分组或堆叠得如此精美。
构建阵列的解决方案:
function BuildStacked(seriesData, GraphCategories) {
var newGraphdata = [];
let i = 0;
GraphCategories = removeDuplicateUsingSet(GraphCategories);
GraphCategories.forEach(function (GraphVal, GraphKey) {
seriesData.forEach(function (SeriesVal, SeiresKey) {
if (SeriesVal.name === GraphVal) {
if (SeriesVal.child === 1) {
newGraphdata[i] = { name: SeriesVal.name, data: [SeriesVal.y], stack: SeriesVal.group };
i++;
}
if (SeriesVal.child === 2) {
newGraphdata[(i - 2)].data.push(SeriesVal.y);
i++;
}
};
});
});
newGraphdata = newGraphdata.filter(function (el) {
return el != null;
});
return newGraphdata;
}