0

I have this kind of array objects right now and i want to make it just a single array of objects. to make it easy for me to iterate through out.

let employees = [
[
    {
        "id": "e0001",
        "loginId": "hpotter",
        "name": "Harry Potter",
        "salary": "1234.00"
    },
    {
        "id": "e0002",
        "loginId": "rwesley",
        "name": "Ron Weasley",
        "salary": "19234.50"
    }
],
[
    {
        "id": "e0003",
        "loginId": "rhagrid",
        "name": "Rubeus Hagrid",
        "salary": "3999.999"
    }
]
]

and this is the code i have right now.

for(var i = 0; i < employees.length; i++) {
  for(var j = 0; j < employees[i].length; j++) {
    console.log(employees[i][j]);
  }
}

i wanna make it into something like this one. to look like just a single array of objects.

let employees = [
[
    {
        "id": "e0001",
        "loginId": "hpotter",
        "name": "Harry Potter",
        "salary": "1234.00"
    },
    {
        "id": "e0002",
        "loginId": "rwesley",
        "name": "Ron Weasley",
        "salary": "19234.50"
    },
    {
        "id": "e0003",
        "loginId": "rhagrid",
        "name": "Rubeus Hagrid",
        "salary": "3999.999"
    }
]
]
  • use [`Array.prototype.flat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat), just `employees.flat()` – kikon Oct 09 '22 at 06:02

0 Answers0