按条件对JSON数组元素值求和并合并为单个结果数组
Sum Numeric Fields in JSON Array and Output Totals in a Single Array
Got it, let's break down how to solve this problem. You need to calculate the sum of specific numeric fields across all objects in your JSON array, then package those totals into a single array (which will contain one object with all the summed values).
Step-by-Step Solution (JavaScript Example)
First, let's use JavaScript since it's commonly used for handling JSON data. Here's a straightforward implementation:
// Your input JSON array (I filled in the missing teamSize value for DCS as 0 for completeness) const inputData = [ {"id":"26","buName":"CAS","totFocCust":23,"totCustResp":10,"totFocProj":160,"totProjResp":13,"wtProjScore":1076.65,"teamSize":246}, {"id":"33","buName":"ECO","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0}, {"id":"28","buName":"DAS","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0}, {"id":"27","buName":"DCS","totFocCust":9,"totCustResp":0,"totFocProj":31,"totProjResp":0,"wtProjScore":0,"teamSize":0} ]; // Initialize a totals object with all target fields set to 0 const fieldTotals = { totFocCust: 0, totCustResp: 0, totFocProj: 0, totProjResp: 0, wtProjScore: 0, teamSize: 0 }; // Iterate through each object and accumulate sums inputData.forEach(item => { // Handle potential missing fields with a fallback to 0 fieldTotals.totFocCust += item.totFocCust || 0; fieldTotals.totCustResp += item.totCustResp || 0; fieldTotals.totFocProj += item.totFocProj || 0; fieldTotals.totProjResp += item.totProjResp || 0; fieldTotals.wtProjScore += item.wtProjScore || 0; fieldTotals.teamSize += item.teamSize || 0; }); // Wrap the totals in an array as requested const finalResult = [fieldTotals]; console.log(finalResult);
Output Result
Running this code will give you the following array with summed values:
[ { "totFocCust": 32, "totCustResp": 10, "totFocProj": 191, "totProjResp": 13, "wtProjScore": 1076.65, "teamSize": 246 } ]
Python Alternative
If you prefer using Python, here's an equivalent implementation:
import json input_data = [ {"id":"26","buName":"CAS","totFocCust":23,"totCustResp":10,"totFocProj":160,"totProjResp":13,"wtProjScore":1076.65,"teamSize":246}, {"id":"33","buName":"ECO","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0}, {"id":"28","buName":"DAS","totFocCust":0,"totCustResp":0,"totFocProj":0,"totProjResp":0,"wtProjScore":0,"teamSize":0}, {"id":"27","buName":"DCS","totFocCust":9,"totCustResp":0,"totFocProj":31,"totProjResp":0,"wtProjScore":0,"teamSize":0} ] field_totals = { "totFocCust": 0, "totCustResp": 0, "totFocProj": 0, "totProjResp": 0, "wtProjScore": 0.0, "teamSize": 0 } for item in input_data: # Use .get() to handle missing fields gracefully field_totals["totFocCust"] += item.get("totFocCust", 0) field_totals["totCustResp"] += item.get("totCustResp", 0) field_totals["totFocProj"] += item.get("totFocProj", 0) field_totals["totProjResp"] += item.get("totProjResp", 0) field_totals["wtProjScore"] += item.get("wtProjScore", 0.0) field_totals["teamSize"] += item.get("teamSize", 0) final_result = [field_totals] print(json.dumps(final_result, indent=2))
Key Notes
- Handling Missing Fields: Both examples include safeguards for missing fields (like the truncated
teamSizein your original data) by falling back to 0, so you won't getNaNor errors. - Scalability: If you have more numeric fields to sum later, just add them to the initial totals object/dictionary and update the accumulation step.
内容的提问来源于stack exchange,提问作者user1662039
相关产品推荐
相关产品推荐

