Javascript JSON:获取 NULL 值,同时汇总找到的每个匹配键的 jsons 数组



我需要在下面的数组中对数值求和。每个数组都有 3 个主键,在此基础上我们匹配另一个 json,它们是"度量"、"职业级别组"和"职业级别"。

var input1 = // trimming down the input due character constraint
[{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"metric": "avgHeadcount",
"careerLevelGroups": [{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerLevel": "Senior Managing Director",
"careerLevels": [{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "Accenture Leadership - GMC",
"careerID": "0-GMC-10000025-GMC"
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "Accenture Leadership - 1",
"careerID": "1-SMD-10000025-CL_1"
}
]
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerLevel": "Managing Director",
"careerLevels": [{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "Accenture Leadership - 2",
"careerID": "2-MD-10000025-CL_2"
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "Accenture Leadership - 3",
"careerID": "3-MD-10000025-CL_3"
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "Accenture Leadership - 4",
"careerID": "4-MD-10000025-CL_4"
}
]
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerLevel": "Senior Manager",
"careerLevels": [{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "5 - Senior Manager",
"careerID": "5-SM-10000050"
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "6 - Senior Manager",
"careerID": "6-SM-10000060"
}
]
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerLevel": "Manager",
"careerLevels": [{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "7 - Manager",
"careerID": "7-M-10000070"
}]
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerLevel": "Consultant",
"careerLevels": [{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "8 - Consultant",
"careerID": "8-C-10000080"
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerL": "9 - Consultant",
"careerID": "9-C-10000090"
}
]
},
{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
"201905": 0,
"201906": 0,
"201907": 0,
"201908": 0,
"201909": 0,
"201910": 0,
"201911": 0,
"format": "number",
"careerLevel": "Analyst",
"careerLevels": [{
"201806": 0,
"201807": 0,
"201808": 0,
"201809": 0,
"201810": 0,
"201811": 0,
"201812": 0,
"201901": 0,
"201902": 0,
"201903": 0,
"201904": 0,
}
]
}
]
}]

var input2 = // iam trimming down this input due to 300000 character limit.
[{
"201609": 0,
"201610": 0,
"201611": 0,
"201612": 0,
"201701": 0,
"201702": 0,
"201703": 0,
"201704": 0,
"201705": 0,
"201706": 0,
"201707": 0,
"201708": 0.5,
"201709": 1,
"201710": 1,
"201711": 1,
"201712": 1,
"201801": 1,
"201802": 1,
"201803": 1.5,
"201804": 2,
"201805": 2,
"201806": 2,
"201807": 2,
"201808": 2,
"201809": 2,
"201810": 2,
"201811": 2,
"201812": 2,
"201901": 2,
"201902": 2,
"201903": 2,
"201904": 2,
"201905": 2,
"201906": 2,
"201907": 2,
"201908": 2,
"201909": 2,
"201910": 2,
"201911": 2,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0,
"fy17Q4": 0.16666666666666666,
"fy18Q1": 1,
"fy18Q2": 1,
"fy18": 0,
"fy18Q3": 1.8333333333333333,
"format": "number",
"metric": "avgHeadCount",
"careerLevelGroups": [{
"201609": 0,
"201610": 0,
"201611": 0,
"201612": 0,
"201701": 0,
"201702": 0,
"201703": 0,
"201704": 0,
"201705": 0,
"201706": 0,
"201707": 0,
"201708": 0,
"201709": 0,
"201710": 0,
"201711": 0,
"201712": 0,
"201801": 0,
"201802": 0,
"201803": 0.5,
"201804": 1,
"201805": 1,
"201806": 1,
"201807": 1,
"201808": 1,
"201809": 1,
"201810": 1,
"201811": 1,
"201812": 1,
"201901": 1,
"201902": 1,
"201903": 1,
"201904": 1,
"201905": 1,
"201906": 1,
"201907": 1,
"201908": 1,
"201909": 1,
"201910": 1,
"201911": 1,
"careerLevel": "Senior Manager",
"careerLevels": [{
"201609": 0,
"201610": 0,
"201611": 0,
"201612": 0,
"201701": 0,
"201702": 0,
"201703": 0,
"201704": 0,
"201705": 0,
"201706": 0,
"201707": 0,
"201708": 0,
"201709": 0,
"201710": 0,
"201711": 0,
"201712": 0,
"201801": 0,
"201802": 0,
"201803": 0.5,
"201804": 1,
"201805": 1,
"201806": 1,
"201807": 1,
"201808": 1,
"201809": 1,
"201810": 1,
"201811": 1,
"201812": 1,
"201901": 1,
"201902": 1,
"201903": 1,
"201904": 1,
"201905": 1,
"201906": 1,
"201907": 1,
"201908": 1,
"201909": 1,
"201910": 1,
"201911": 1,
"careerID": "6-SM-10000060",
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0,
"fy17Q4": 0,
"fy18Q1": 0,
"fy18Q2": 0,
"fy18": 0,
"fy18Q3": 0.8333333333333334,
"format": "number"
}],
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0,
"fy17Q4": 0,
"fy18Q1": 0,
"fy18Q2": 0,
"fy18": 0,
"fy18Q3": 0.8333333333333334,
"format": "number"
},
{
"201609": 0,
"201610": 0,
"201611": 0,
"201612": 0,
"201701": 0,
"201702": 0,
"201703": 0,
"201704": 0,
"201705": 0,
"201706": 0,
"201707": 0,
"201708": 0.5,
"201709": 1,
"201710": 1,
"201711": 1,
"201712": 1,
"201801": 1,
"201802": 1,
"201803": 1,
"201804": 1,
"201805": 1,
"201806": 1,
"201807": 1,
"201808": 1,
"201809": 1,
"201810": 1,
"201811": 1,
"201812": 1,
"201901": 1,
"201902": 1,
"201903": 1,

}
]
}]

但是我的输出如下,其中包括 NULL,我无法弄清楚为什么会发生这种情况。

输出=

[{
"201609": null,
"201610": null,
"201611": null,
"201612": null,
"201701": null,
"201702": null,
"201703": null,
"201704": null,
"201705": null,
"201706": null,
"201707": null,
"201708": null,
"201709": null,
"201710": null,
"201711": null,
"201712": null,
"201801": null,
"201802": null,
"201803": null,
"201804": null,
"201805": null,
"201806": 2,
"201807": 2,
"201808": 2,
"201809": 2,
"201810": 2,
"201811": 2,
"201812": 2,
"201901": 2,
"201902": 2,
"201903": 2,
"201904": 2,
"201905": 2,
"201906": 2,
"201907": 2,
"201908": 2,
"201909": 2,
"201910": 2,
"201911": 2,
"format": "number",
"metric": "avgHeadcount",
"careerLevelGroups": [{
"201609": null,
"201610": null,
"201611": null,
"201612": null,
"201701": null,
"201702": null,
"201703": null,
"201704": null,
"201705": null,
"201706": null,
"201707": null,
"201708": null,
"201709": null,
"201710": null,
"201711": null,
"201712": null,
"201801": null,
"201802": null,
"201803": null,
"201804": null,
"201805": null,
"201806": 1,
"201807": 1,
"201808": 1,
"201809": 1,
"201810": 1,
"201811": 1,
"201812": 1,
"201901": 1,
"201902": 1,
"201903": 1,
"201904": 1,
"201905": 1,
"201906": 1,
"201907": 1,
"201908": 1,
"201909": 1,
"201910": 1,
"201911": 1,
"format": "number",
"careerLevel": "Senior Managing Director",
"careerLevels": [{
"201609": null,
"201610": null,
"201611": null,
"201612": null,
"201701": null,
"201702": null,

}]

我正在做的如下:

function processJsonToSumValues(input, addingArray) {
console.log("input", input);
console.log("addingArray",addingArray);
input.forEach(function(item, index) {
console.log("item", item);
console.log("index",index);
for (let key in item) {
// for numbers onlyyy
if (typeof item[key] === 'number') {
addingArray[index][key] = addingArray[index][key] + item[key]
}
if (Array.isArray(item[key])) {
console.log("input[index][key]", input[index][key]);
console.log("addingArray[index][key]",addingArray[index][key]);
processJsonToSumValues(input[index][key], addingArray[index][key])
}
}
})
}
processJsonToSumValues(input2, input1);
console.log(JSON.stringify(input1));

只是设置默认值的问题...

NaN + 0 = NaN

因此,当您使用addingArray[index][key] = addingArray[index][key] + item[key]并且在第一次运行/迭代时未设置addingArray[index][key]值时,不会设置该值。

若要解决此问题,请使用addingArray[index][key] = (addingArray[index][key] || 0) + item[key]设置为 0(如果未设置(。

function processJsonToSumValues(input, addingArray) {
console.log("input", input);
console.log("addingArray",addingArray);
input.forEach(function(item, index) {
console.log("item", item);
console.log("index",index);
for (let key in item) {
// for numbers onlyyy
if (typeof item[key] === 'number') {
addingArray[index][key] = (addingArray[index][key] || 0) + item[key]
}
if (Array.isArray(item[key])) {
console.log("input[index][key]", input[index][key]);
console.log("addingArray[index][key]",addingArray[index][key]);
processJsonToSumValues(input[index][key], addingArray[index][key])
}
}
})
}

相关内容

  • 没有找到相关文章

最新更新