首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >合并数组键

合并数组键
EN

Stack Overflow用户
提问于 2019-11-11 19:58:37
回答 2查看 74关注 0票数 0

下面的代码遍历两个数组,并在运行后输出以下内容。

代码语言:javascript
运行
复制
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);
}

这是代码生成的最终数组

代码语言:javascript
运行
复制
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中。因为名称包含已插入的项

代码语言:javascript
运行
复制
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"}

因此,我尝试实现的输出或多或少采用以下格式

代码语言:javascript
运行
复制
{
        name: 'AdminFee',
        data: [AdminFee.data, AdminFeeVat.data],
        stack: '2019-10'
    }, {
        name: 'AdminFee',
        data: [AdminFee.data, AdminFeeVat.data],
        stack: '2019-11'
    }

当AdminFee、AdminFeeVat具有相同的堆栈/日期时,应在数据中进行联接

所以最终的结果应该是

代码语言:javascript
运行
复制
0: {name: "AdminFee", data: Array(2), stack: "2019-10"}
1: {name: "AdminFee", data: Array(2), stack: "2019-11"}
EN

回答 2

Stack Overflow用户

发布于 2019-11-11 20:49:41

您需要使用Object并为结果的每一行定义一个键,而不是使用array.push()。最后,如果需要将数组作为输出,请将其转换为数组。例如:

代码语言:javascript
运行
复制
newGraphdata.push({ 'name': SeriesVal.name, 'data': [SeriesVal.y], 'stack': SeriesVal.group });

将转换为:

代码语言:javascript
运行
复制
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

票数 0
EN

Stack Overflow用户

发布于 2019-11-13 14:27:20

这个修复是半脏的,但它是有效的,因为我现在或多或少以我需要的格式获得了数据,只是Highcharts还没有很好地对数据进行分组或堆叠。

我有一个构建数组的解决方案:

代码语言:javascript
运行
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58800930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档