Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove JSON element with index?

So let's say I have a JSON called foo:

{
     "key" : "value",
     "one" : "two"
}

I want to delete the first element, so with the index of 0. But I don't know that it's key.

I tried using delete foo[0] but it didn't work. It outputs true but it doesn't actually delete it.

Is there a way to do this without looping through it?

like image 793
Troughy Avatar asked Dec 03 '22 17:12

Troughy


1 Answers

Get the key with Object.keys(JSON)[0] and then remove it.

like image 106
Nils Schwebel Avatar answered Dec 20 '22 15:12

Nils Schwebel