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

92
Views
Add an Array to an JSON Object

In my API im receiving a JSON with 2 arrays, one Employees and another with Managers, the goal is to add the Managers to each Employee, knowing that the 1st index of the managers corresponds to the 1st index of the employees and so on:

This is an example of the request to the API

{
"employees" : [ {
    "code" : "111111",
    "name" : "Zé"
},
{
    "code" : "222222",
    "name" : "João"
},
{
    "code" : "444444",
    "name" : "António"
}],
"managers" : [
    [
        {
            "name": "vitor",
            "level" : "1"
        },
        {
            "name": "Antonio",
            "level" : "2"
        }
    ],
    [
        {
            "name": "Jose",
            "level" : "1"
        },
        {
            "name": "Ines",
            "level" : "2"
        }
    ],
    [
        {
            "name": "Luis",
            "level" : "1"
        },
        {
            "name": "Ana",
            "level" : "2"
        }
    ]
]

}

and my goal is to get something like this:

[
    {
        "code": "111111",
        "name": "Zé",
        "managers": [
            {
                "name": "vitor",
                "level": "1"
            },
            {
                "name": "Antonio",
                "level": "2"
            }
        ]
    },
    {
        "code": "222222",
        "name": "João",
        "managers": [
            {
                "name": "Jose",
                "level": "1"
            },
            {
                "name": "Ines",
                "level": "2"
            }
        ]
    },
    {
        "code": "444444",
        "name": "António",
        "managers": [
            {
                "name": "Jose",
                "level": "1"
            },
            {
                "name": "Ines",
                "level": "2"
            }
        ]
    }
]

I already tried some things but i can't get the expected result :( Hope you can help me!! Thanks

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

0

function associate_employee_managers({ employees, managers }) {
    return employees && employees.length
        && employees
            .map((emp, index) => {
                return {
                    ...emp,
                    ["managers"]: managers && managers[index] || []
                };
            }) || {};
}

Considerations

There is 1-1 correspondence(based on index) between entries inside employees array and managers array


Illustration

function associate_employee_managers({
  employees,
  managers
}) {
  return employees && employees.length &&
    employees
    .map((emp, index) => {
      return {
        ...emp,
        ["managers"]: managers && managers[index] || []
      };
    }) || {};
}

const jsonResponse = {
  "employees": [{
      "code": "111111",
      "name": "Zé"
    },
    {
      "code": "222222",
      "name": "João"
    },
    {
      "code": "444444",
      "name": "António"
    }
  ],
  "managers": [
    [{
        "name": "vitor",
        "level": "1"
      },
      {
        "name": "Antonio",
        "level": "2"
      }
    ],
    [{
        "name": "Jose",
        "level": "1"
      },
      {
        "name": "Ines",
        "level": "2"
      }
    ],
    [{
        "name": "Luis",
        "level": "1"
      },
      {
        "name": "Ana",
        "level": "2"
      }
    ]
  ]
}

console.log(associate_employee_managers(jsonResponse));


WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

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!