Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Modify array of objects to replace with additional properties if null

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
                }
            }
        ]
    }
  ]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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)
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!