Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

I have tried to search the solution alot but no one really helped me.

I have a javascript object:

trucks_obj
= Object {95: _.Kd, 96: _.Kd}

95: _.Kd
96: _.Kd
__proto__: Object

If the if condition is true, I just want to remove element from trucks_obj with specific ID. e.g. I need to remove element with 95 index. I have already tried splice() as well as slice() but nothing worked.

Deletion Code:

$.each(trucks_obj,function(i,e){
            if(($.inArray(i, trucks_arr)) === -1){
                trucks_obj.splice(i,1);
            }
        });
share|improve this question

marked as duplicate by squint javascript Dec 10 '15 at 12:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 5 down vote accepted

Just use delete:

delete trucks_obj['95']
share|improve this answer
    
I tried but it didn't worked. is it possible to delete an element of object on which we running, $.each()? – Nutty Programmer Dec 10 '15 at 12:11
    
var keys = Object.keys(trucks_obj); will return you array of keys . keys[i] will give you the current key. then you can use delete trucks_obj[keys[i]]; – Shubham Dec 10 '15 at 12:14
    
it's not a good idea to delete the reference you are actually iterate on. so no. a workaround may be another array with the properties to delete and iterate over the array and delete the properties of the object. – Nina Scholz Dec 10 '15 at 12:14
    
@Shubham, that works. – Nina Scholz Dec 10 '15 at 12:15
    
delete is a native Javascript function. A jquery object is not 100% compatible to a plain Javascript object – reporter Dec 10 '15 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.