-2

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.

5
  • what about Array#shift? Commented Feb 27, 2021 at 8:56
  • It gives me undefined when i log the resulting array. Commented Feb 27, 2021 at 9:09
  • please add the array and wanted result in text form. Commented Feb 27, 2021 at 9:11
  • Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output, preferably in a Stacksnippet Commented Feb 27, 2021 at 9:13
  • The array with the objects seems to be inside another array Commented Feb 27, 2021 at 11:41

3 Answers 3

0

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)}`)

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

4 Comments

It gives me undefined when i log the resulting array.
now it is working but the array remains with the parent key 0, it didn´t remove that key.
The shift method removes and returnes the first element of your array (all elements are beeing shifted to the left). So you just need to do <yourArray>.shift() and then examine the modifed array.
I'll edit my answer with an example so you can run it.
0

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' }
  }
]

Comments

0

Try using shift method, like:

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

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.