JAVASCRIPT remove parent key from array

517 Views Asked by At

I have an array and i need to remove the top most 0 key, and leave the array with the following structure:

[array(24)]

0:{...}

1:{...}

2:{...}

3:{...}

until last key (24)

Right now this is how the array is:

enter image description here

I have tried many methods like assign, flat, array_shift, Object.keys(), Object.values(), and others without any luck.

3

There are 3 best solutions below

4
Tamir Nakar On BEST ANSWER

There is a designated function for it. Try myArray.shift()

const myArray = [{a:1}, {b:2}, {c:3}]
const firstElement = myArray.shift();
console.log(`first element: ${JSON.stringify(firstElement)}`)
console.log(`modified Array: ${JSON.stringify(myArray)}`)

0
Richard Rublev On

You can use Object.values

myarray = {
    Array24 :
    {
        '0': { category :'saude'},
        '1': {price: '40'},
        '2': { category :'saude'},
        '3': {price: '40'},
        '4': { category :'saude'},
        '5': {price: '40'},
    }
}

console.log(Object.values(myarray));

You will get

  {
    '0': { category: 'saude' },
    '1': { price: '40' },
    '2': { category: 'saude' },
    '3': { price: '40' },
    '4': { category: 'saude' },
    '5': { price: '40' }
  }
]
0
AdriSolid On

Try using shift method, like:

const arr = [1, 2, 3, 4, 5];
arr.shift();
console.log(arr);