Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript remove values from (inside) object literal

Tags:

javascript

I'm trying to remove an object from (inside?) a object literal. But I cant figure out why it doenst work.

var data= 
{
   "car": 3,
   "boat": 2
};

data.pop(); //should remove last one?
data.splice(1, 1); // removes element with index 1?

What am I doing wrong here? And is it posible you delete the item by name? Let say remove boat.

like image 862
Rob Avatar asked Dec 18 '25 23:12

Rob


1 Answers

Since its a Object not an array, delete should work

Like this:

var data= 
{
   "car": 3,
   "boat": 2
};

alert(data["boat"]);
delete data["boat"];
alert(data["boat"]);

See it in action :
JSFiddle

like image 189
gideon Avatar answered Dec 21 '25 11:12

gideon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!