下面的代码遍历两个数组,并在运行后输出以下内容。
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"}发布于 2019-11-11 20:49:41
您需要使用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
发布于 2019-11-13 14:27:20
这个修复是半脏的,但它是有效的,因为我现在或多或少以我需要的格式获得了数据,只是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;
}https://stackoverflow.com/questions/58800930
复制相似问题