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

3
  • What exactly do you want? To have all the objects in one single array? Commented Jan 25, 2023 at 6:31
  • 1
    perhaps .flat Commented Jan 25, 2023 at 6:32
  • yes , i need the all objects in single array Commented Jan 25, 2023 at 6:33

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.