Home > Net >  Modify array of objects to replace with additional properties if null
Modify array of objects to replace with additional properties if null

Time:09-17

I have an array of object as follows

let data = [
    {
        vertical_name: "CORE",
        projects: [
            {
                name: "Alpha",
                id: 187,
                current_result: null,
            }
        ]
    },
    {
        vertical_name: "release",
        projects: [
            {
                name: "Beta",
                id: 27,
                current_result: {
                    success_percentage: 37,
                    failure_percentage: 25,
                    skip_percentage: 36
                }
            },
            {
                name: "Charlie",
                id: 47,
                current_result: {
                    success_percentage: 37,
                    failure_percentage: 25,
                    skip_percentage: 36
                }
            }
        ]
    }
  ]

As you can see current_result is null at one place. So i have to modify data to show that current_result is not tested. So in this case, instead of showing null, i want to show as follows. not_tested is 100 and skip, failure and success percentage is 0 in this case.

current_result: {
  success_percentage: 0,
  failure_percentage: 0,
  skip_percentage: 0,
  not_tested: 100
}

Also on other values of current_result, since it is not null and has success, failure and skip values, not_tested should be 0 for this.

The final result should look something like this. can someone please let me know how to achieve this as it is slightly complicated for me to come to a definitive answer

result = [
    {
        vertical_name: "CORE",
        projects: [
            {
                name: "Alpha",
                id: 187,
                current_result: {
                    success_percentage: 0,
                    failure_percentage: 0,
                    skip_percentage: 0,
                    not_tested: 100
                }
            }
        ]
    },
    {
        vertical_name: "release",
        projects: [
            {
                name: "Beta",
                id: 27,
                current_result: {
                    success_percentage: 37,
                    failure_percentage: 25,
                    skip_percentage: 36,
                    not_tested: 0
                }
            },
            {
                name: "Charlie",
                id: 47,
                current_result: {
                    success_percentage: 37,
                    failure_percentage: 25,
                    skip_percentage: 36,
                    not_tested: 0
                }
            }
        ]
    }
  ]

CodePudding user response:

You could loop into data's projects array and insert keys you want in this way:

let data = [ { vertical_name: "CORE", projects: [ { name: "Alpha", id: 187, current_result: null, } ] }, { vertical_name: "release", projects: [ { name: "Beta", id: 27, current_result: { success_percentage: 37, failure_percentage: 25, skip_percentage: 36 } }, { name: "Charlie", id: 47, current_result: { success_percentage: 37, failure_percentage: 25, skip_percentage: 36 } } ] } ]
  
  data.forEach(dat => {
     dat.projects.forEach(prj => {
         if (!prj.current_result) {
            prj.current_result = {
                success_percentage: 0,
                failure_percentage: 0,
                skip_percentage: 0,
                not_tested: 100
            };
         }
         else {
            prj.current_result.not_tested = 0;
         }
     })
  })
  
  console.log(data)

CodePudding user response:

Using Array#map without mutating the original data

const data = [{"vertical_name":"CORE","projects":[{"name":"Alpha","id":187,"current_result":null}]},{"vertical_name":"release","projects":[{"name":"Beta","id":27,"current_result":{"success_percentage":37,"failure_percentage":25,"skip_percentage":36}},{"name":"Charlie","id":47,"current_result":{"success_percentage":37,"failure_percentage":25,"skip_percentage":36}}]}];

const formatted = data.map(item => Object.assign(item, {
  projects: item.projects.map(project => {
    if (project.current_result === null) {
      project.current_result = {
        success_percentage: 0,
        failure_percentage: 0,
        skip_percentage: 0,
        not_tested: 100
      };
    } else project.current_result.not_tested = 0;

    return project;
  })
}));

console.log(formatted);

CodePudding user response:

let data = [{vertical_name: "CORE",projects: [{name: "Alpha",id: 187, current_result: null,}]},{vertical_name: "release", projects: [{               name: "Beta", id: 27, current_result: {                    success_percentage: 37, failure_percentage: 25,                 skip_percentage: 36}},{name: "Charlie", id: 47, current_result: {                     success_percentage: 37, failure_percentage: 25,                    skip_percentage: 36}}]}]  

const newData = data.map((element)=>{
  return element.projects.map((project)=>{
       if(project.current_result === null){
         return {...project, current_result: {success_percentage: 0,
                                              failure_percentage: 0,
                                              skip_percentage: 0,
                                              not_tested: 100
                                             }
               }
      }
      return {...project, current_result: {...project.current_result, not_tested: 0} }
}) 
})

console.log(newData)

you can try this code :

  data.map((element)=>{
  element.projects.map((project)=>{
       if(project.current_result === null){
         return {...project, current_result: {success_percentage: 0,
                                              failure_percentage: 0,
                                              skip_percentage: 0,
                                              not_tested: 100
                                             }
               }
      }
      return {...project, current_result: {...project.current_result, not_tested: 0} }
}) 
})

CodePudding user response:

You can filter out your result data and assign a default value.

const result = data.map((item) => {
          const projects = item.projects.map((project) => {
            if (!project?.current_result) {
              return {
                ...project,
                current_result: {
                  success_percentage: 0,
                  failure_percentage: 0,
                  skip_percentage: 0,
                  not_tested: 100,
                },
              };
            } else {
              return project;
            }
          });

     console.log(result)
  • Related