Home > Net >  How To Remove Nested Array In React Native
How To Remove Nested Array In React Native

Time:03-09

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 for pushing into the array :

    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 can I achieve this?

CodePudding user response:

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)

  • Related