从对象数组中推入数组中的值

Posted

技术标签:

【中文标题】从对象数组中推入数组中的值【英文标题】:Push values in an array from an array of objects 【发布时间】:2021-11-06 04:59:10 【问题描述】:

我有一个对象数组,如下所示。我想基本上循环遍历projects.current_result 并获取所有值。

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
                
            
        ]
    
  ]

例如我想获取每个项目的success 值并将所有这些值放入一个数组中。所以在这种情况下success= [0,50,37] 同样,我想为failure, skip and untested 输入值

failure=[0,20,25]
skip=[0,36,40]
untested=[100,0,0]

我尝试使用以下代码实现这一点,但不知何故我无法获得所需的结果。请提供有关如何更正此代码的任何建议

let alteredResponse = data.forEach(prj =>  
     prj.reduce((result, 
    count
  ) => 
    result.success.push(count.success|| 0);
    result.failure.push(count.failure || 0);
    result.skip.push(count.skip || 0);
    result.untested.push(count.untested || 0);
    return result;
  , 
    success: [],
    failure: [],
    skip: [],
    untested: []
  )
  );

【问题讨论】:

【参考方案1】:

您可以通过两个forEach 实现此目的

逻辑

循环通过data 中的每个节点 从data 中的节点循环遍历projects 中的每个节点 将successfailureskipuntested节点从project节点推送到相应的结果节点。

工作小提琴

const 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]];

const result = 
  success: [],
  failure: [],
  skip: [],
  untested: [],


const alteredResponse = data.forEach(prj => 
  prj.projects.forEach((project) => 
    result.success.push(project.current_result.success);
    result.failure.push(project.current_result.failure);
    result.skip.push(project.current_result.skip);
    result.untested.push(project.current_result.untested);
  )
);
console.log(result);

【讨论】:

【参考方案2】:

你可以试试这个方法

获得projects的第一张地图 展平projects 映射projects 以获取current_results reduce 将所有结果合并到一个最终对象中

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, , , ], , ]

const res = data
  .map(( projects ) => projects)
  .flat()
  .map(( current_result ) => current_result)
  .reduce((acc, el) => 
    for (const [key, value] of Object.entries(el)) 
      if (!acc[key]) 
        acc[key] = [value]
       else 
        acc[key].push(value)
      
    
    return acc
  , )

console.log(res)

【讨论】:

这会执行 3 级循环,这可以通过更少的循环数来实现【参考方案3】:

使用预定义的数组可能更容易和可读:

const 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, , , ], , ];

const success = [];
const failure = [];
const skip = [];
const untested = [];

data.forEach((prj) => 
  prj.projects.forEach(( current_result ) => 
    success.push(current_result.success || 0);
    failure.push(current_result.failure || 0);
    skip.push(current_result.skip || 0);
    untested.push(current_result.untested || 0);
  );
);

console.log("success", success);
console.log("failure", failure);
console.log("skip", skip);
console.log("untested", untested);

【讨论】:

【参考方案4】:

const 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
        
      
    ]
  
];

const result = 
  success: [],
  failure: [],
  skip: [],
  untested: []
;
data.forEach((prj) => 
  prj.projects.reduce((_,  current_result ) => 
    result.success.push(current_result.success || 0);
    result.failure.push(current_result.failure || 0);
    result.skip.push(current_result.skip || 0);
    result.untested.push(current_result.untested || 0);
    return result;
  , result);
);

console.log(result);

【讨论】:

以上是关于从对象数组中推入数组中的值的主要内容,如果未能解决你的问题,请参考以下文章

为什么我的对象没有被推入不同的数组?

创建数组并将对象推入数组后,对象是[Object],而不是实际数据

将对象推入 Mongoose 中的数组模式

推入 mongoose 对象内部的数组

Angular2,Typescript:如何将对象数组推入另一个对象数组,其中只有几个对象字段

Meteor JS如何从集合中的数组中获取特定对象?