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

295
Views
Format non nested object to nested object in JavaScript

We get a non nested object

obj = {
        "your-email": "some@email.com",
        "prename": "Jane Doe",
        "service__1": "Google",
        "login__1": "support@google.com",
        "password__1": "PW1",
        "service__2": "Microsoft",
        "login__2": "admin@email.com",
        "password__2": "PW12!",
        "service__3": "Stackoverflow",
        "login__3": "supportadmin@email.com",
        "password__3": "3PW2!",
        "DPA": "1"
    }

We would like to transform this object into following structure:

nestedOBJ =         
{
        "your-email": "some@email.com",
        "prename": "Jane Doe",
        "DPA": "1",
        "service1": 
                  {
                    "service__1": "Google",
                    "login__1": "support@google.com",
                    "password__1": "PW1"
                  },
        "service2": 
                  {
                    "service__2": "Microsoft",
                    "login__2": "admin@email.com",
                    "password__2": "PW12!"
                  },
        "service3": 
                  {
                    "service__3": "mySQL",
                    "login__3": "supportadmin@email.com",
                    "password__3": "3PW2!"
                  },
        "service4": 
                  {
                    "...":"..."
                  }
}

The following code has been tried without success:

let res = {}
let length = Object.keys(obj).length -3;
for (let i = 0; i < length  ; i + 3){
console.log(steps.trigger.event.body[0]["service__"+i])
  let onestedOBJ = {
    ["creds"+i]:steps.trigger.event.body[0]["service__"+i],
    ["creds"+i]:steps.trigger.event.body[0]["login__"+i],
    ["creds"+i]:steps.trigger.event.body[0]["password__"+i]
  }  
console.log(nestedOBJ)
}

We didn't include the DPA, your-email and prename. Because the for loop didn't even work.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const nested = {};

for (const [key, value] of Object.entries(obj)) {
    const [, num] = key.split("__");

    if (num) {
        const parentKey = `service${num}`;

        if (nested[parentKey] === undefined) {
            nested[parentKey] = {};
        }
        
        nested[parentKey][key] = value;
    } else {
        nested[key] = value;
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

Array#reduce is one way to go about it

let obj = {
  "your-email": "some@email.com",
  "prename": "Jane Doe",
  "service__1": "Google",
  "login__1": "support@google.com",
  "password__1": "PW1",
  "service__2": "Microsoft",
  "login__2": "admin@email.com",
  "password__2": "PW12!",
  "service__3": "Stackoverflow",
  "login__3": "supportadmin@email.com",
  "password__3": "3PW2!",
  "DPA": "1"
}

let subitems = ["service", "login", "password"];
let nestedOBJ = Object.entries(obj).reduce((b, a) => {
  let keya = a[0].split("__");
  if (subitems.indexOf(keya[0]) > -1) {
    b[`service${keya[1]}`] = b[`service${keya[1]}`] || {}
    b[`service${keya[1]}`][keya[0]] = a[1];
  } else b[a[0]] = a[1];
  return b;
}, {});
console.log(nestedOBJ);

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!