Home > Blockchain >  Add an Array to an JSON Object
Add an Array to an JSON Object

Time:03-10

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

CodePudding user response:

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

CodePudding user response:

You can simply use a for loop to reformat your data. For example:

const data = {
  "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"
      }
    ]
  ]
}

let newObj = []

const dataLength = data.employees.length;

for(let i = 0; i < dataLength; i  ){
  newObj.push({
    "code": data.employees[i].code,
    "name": data.employees[i].name,
    "managers": data.managers[i]
  })
}

// This is return the code as JS object
console.log(newObj)


// If you want to convert it to JSON
let newObjAsJson = JSON.stringify(newObj)
console.log(newObjAsJson)

CodePudding user response:

let oldJson = {
  "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"
      }
    ]
  ]
}

let newJson = []


for (let i = 0; i < oldJson.employees.length; i  ) {
  newJson.push({
    "code": oldJson.employees[i].code,
    "name": oldJson.employees[i].name,
    "managers": oldJson.managers[i]

  })
}


console.log(newJson)

  • Related