Home > Net >  Push values in an array from an array of objects
Push values in an array from an array of objects

Time:09-17

I have an array of object as follows. I want to basically loop through the projects.current_result and get all the values.

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

for e.g. i want to get success values of each of the projects and put all of those values in an array. So in this case success= [0,50,37] Similarly, i want to put values for failure, skip and untested

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

I tried achieving that with below code but somehow i am not able to get desired result. Any advice on how to correct this code please

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: []
  })
  });

CodePudding user response:

You can achieve this with two forEach

Logic

  • Loop though each nodes in data
  • Loop through each nodes in projects from the nodes in data
  • Push success, failure, skip, untested nodes from project node to respective result node.

Working Fiddle

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);

CodePudding user response:

You could try this approach

  • first map to get the projects
  • flatten the projects
  • map the projects to get the current_results
  • reduce to combine all the results into one final object

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)

CodePudding user response:

Using predefined arrays could be much easier and readable:

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);

CodePudding user response:

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);

  • Related