0

I need to fetch all objects from the array of array , but i am unable to find out that, i there any array method from which we can find out the objects from the array of array.

const array1 = [
[{
   id:1,
   name:"test1"
 },
{
   id:2,
   name:"test2"
},
{
   id:3,
   name:"test3"
},
],
[{
   id:4,
   name:"test4"
 },
{
   id:5,
   name:"test5"
},
{
   id:6,
   name:"test6"
},
],
[{
   id:7,
   name:"test7"
 },
{
   id:8,
   name:"test8"
},
{
   id:9,
   name:"test9"
},
],

]

i just need to that i can get the all objects in a array, using the array methods

1 Answers1

0

Just uses the flat method.

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

const array1 = [
  [{
      id: 1,
      name: "test1",
    },
    {
      id: 2,
      name: "test2",
    },
    {
      id: 3,
      name: "test3",
    },
  ],
  [{
      id: 4,
      name: "test4",
    },
    {
      id: 5,
      name: "test5",
    },
    {
      id: 6,
      name: "test6",
    },
  ],
  [{
      id: 7,
      name: "test7",
    },
    {
      id: 8,
      name: "test8",
    },
    {
      id: 9,
      name: "test9",
    },
  ],
];
let result = array1.flat()
console.log(result);
Neha Soni
  • 3,935
  • 2
  • 10
  • 32