Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How To Remove Nested Array In React Native

I don’t know how to remove the nested array in react native. example :

Array [
  Array [
    Array [
      "77",
      undefined,
      "Double Pixel Chart",
      "c1",
      "Chart",     
    ],
    Array [
      "78",
      undefined,
      "Heikin Ashi Chart",
      "c2",
      "Chart",      
    ],               
  ],
]

What I want is just this array structure :

    Array [
      "77",
      undefined,
      "Double Pixel Chart",
      "c1",
      "Chart",     
    ],
    Array [
      "78",
      undefined,
      "Heikin Ashi Chart",
      "c2",
      "Chart",      
    ],               

this is my code when push array :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for (let menu of response.data.Data) {                
        apiChartingMenu = new ApiChartingMenuModel (
            menu.idx,
            menu.shortDescription,
            menu.titlecommand,
            menu.command,
            menu.commandtype,                     
        );                            
        
        data.push(Object.values(apiChartingMenu));
    }  

How to do it ? Thank You

>Solution :

You can use plain Javascript by using array.flat(). For your case this could be done as follows.

array = [

   [
      [
      "77",
      undefined,
      "Double Pixel Chart",
      "c1",
      "Chart",     
     ],
     [
      "78",
      undefined,
      "Heikin Ashi Chart",
      "c2",
      "Chart",      
      ],        
   ]
]


console.log(array.flat())

In your case you could make it work by either using

data.push(Object.values(apiChartingMenu).flat());

or by not introducing the object ApiChartingMenuModel at all, since you are just using its values anyway. This could also be done as follows.

// dummy menu object
menu = {

   idx: 77,
   shortDescription: "description",
   titlecommand: undefined,
   command: undefined,
   commandtype: undefined
}


data = []

data.push([menu.idx, menu.shortDescription, menu.titlecommand, menu.command, menu.commandtype])

console.log(data)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading