You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从嵌套对象数组中提取各项目current_result的字段值并整理为对应数组?

Fixing Your Project Result Extraction Code

Let's break down what was going wrong with your original code and fix it step by step:

Key Issues in Your Code:

  1. forEach doesn't return a value: You're assigning the result of data.forEach() to alteredResponse, but forEach always returns undefined—so this variable won't hold your desired arrays.
  2. Incorrect reduce target: You're calling reduce on prj (each vertical object in data), but you should be reducing over prj.projects (the array of projects inside each vertical).
  3. Wrong destructuring: You're trying to extract count from each project, but the property you need is current_result.

Corrected Code

Here's a revised version that will give you the exact arrays you're looking for:

let data = [
  { vertical_name: "CORE", projects: [ { name: "Alpha", id: 187, current_result: { success: 0, failure: 0, skip: 0, untested: 100 } } ] },
  { vertical_name: "release", projects: [ { name: "Beta", id: 27, current_result: { success: 50, failure: 20, skip: 36, untested: 0 } }, { name: "Charlie", id: 47, current_result: { success: 37, failure: 25, skip: 40, untested: 0 } } ] }
];

// Initialize our result object with empty arrays
const result = data.reduce((acc, vertical) => {
  // Loop through each project in the current vertical
  vertical.projects.forEach(project => {
    // Extract the values from current_result
    const { success, failure, skip, untested } = project.current_result;
    
    // Push each value to its respective array (fallback to 0 if undefined)
    acc.success.push(success || 0);
    acc.failure.push(failure || 0);
    acc.skip.push(skip || 0);
    acc.untested.push(untested || 0);
  });
  return acc;
}, { success: [], failure: [], skip: [], untested: [] });

console.log(result.success);   // [0, 50, 37]
console.log(result.failure);  // [0, 20, 25]
console.log(result.skip);     // [0, 36, 40]
console.log(result.untested); // [100, 0, 0]

How This Works:

  1. reduce on data: We use reduce to build our result object starting with empty arrays for each metric.
  2. Iterate over projects: For each vertical, we loop through its projects array.
  3. Extract and push values: For each project, we destructure the current_result properties and push them into the corresponding arrays in our accumulator.
  4. Fallback values: The || 0 ensures that if a property is missing, we add 0 instead of undefined.

Alternative (Using Nested reduce)

If you prefer to use reduce for both levels instead of forEach, here's another version:

const result = data.reduce((acc, vertical) => {
  return vertical.projects.reduce((innerAcc, project) => {
    const { success, failure, skip, untested } = project.current_result;
    innerAcc.success.push(success || 0);
    innerAcc.failure.push(failure || 0);
    innerAcc.skip.push(skip || 0);
    innerAcc.untested.push(untested || 0);
    return innerAcc;
  }, acc);
}, { success: [], failure: [], skip: [], untested: [] });

Both approaches will produce the exact output you're expecting.

内容的提问来源于stack exchange,提问作者Aren Trot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:07:37