我是Nodejs和typescript的新手。我有两个以下的jsons:
json1 =
{
"201809": 2,
"metric": "headcount",
"quarter1": 60,
"careerLevelsGroups": [{
"201809": 2,
"quarter1": 60,
"careerLevels": [{
"201809": 2,
"careerId": "careerId1",
"quarter1": 60,
},
{
"201809": 2,
"careerId": "careerId2",
"quarter1": 50,
}
]
}]
}
json2 =
{
"201809": 3,
"metric": "headcount",
"quarter1": 100,
"careerLevelsGroups": [{
"201809": 7,
"quarter1": 40,
"careerLevels": [{
"201809": 9,
"careerId": "careerId1",
"quarter1": 30,
},
{
"201809": 8,
"careerId": "careerId2",
"quarter1": 30,
}
]
}]
}
我想将与相同键相关联的所有数值相加并生成一个json,其总和值为json1和json2。
result =
{
"201809": 5,
"metric": "headcount",
"quarter1": 160,
"careerLevelsGroups": [{
"201809": 9,
"quarter1": 100,
"careerLevels": [{
"201809": 11,
"careerId": "careerId1",
"quarter1": 90,
},
{
"201809": 10,
"careerId": "careerId2",
"quarter1": 80,
}
]
}]
}
我正在尝试使用循环,但是我有太多这样的元素需要总结,所以请你建议在Node.js中使用更优化的方法
答案 0 :(得分:1)
我整理了一个// send command method
STRING szCmdPath, szCmdLine ;
szCmdPath = "C:\\WINDOWS\\system32\\cmd.exe";
szCmdLine = "/c wslconfig /l > D:\\output.txt";
LaunchAppAndWait(szCmdPath, szCmdLine, WAIT);
函数,该函数使用递归来处理JSON对象的成员。如果它检测到combine()
它将它组合在一起。如果它检测到"number"
,则会应用递归来搜索其中的更多"object"
或"number"
。对于任何其他类型(例如"object"
),它只是将值保留为原样。
"string"
运行脚本会产生所需的输出。它应该是一个简单的执行,迭代一系列这些JSON对象来生成所需的结果。