Reputation: 5601
I have an array and object in js:
var number = [23,234,654,3234];
var detail = {
23:"John",
234:"Doe",
654:50,
3234:"blue"
};
Then using var remove = number.shift()
, I can get the first value in the array (in this case, 23) and remove it from the array.
I am trying to remove the corresponding property from the object: In this case, it will be 23:"John"
.
I tried delete detail.remove;
but no luck.
Any suggestions?
Thanks
Upvotes: 1
Views: 315
Reputation: 115242
According to MDN the delete operator is followed by an expression that should evaluate to a property reference. Your example delete detail.remove
is in fact correct.
However, if you want to access a property programmatically (or numbered property, such as 23), use bracket notation.
// with variable
delete detail[remove];
// with string or integer
delete detail[23];
Upvotes: 1
Reputation: 36
you can't use variable in obj.variable, the right way is obj[variable].
So try this instead:
delete detail[remove];
Upvotes: 1