-3

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);
            }
        });
0

1 Answer 1

5

Just use delete:

delete trucks_obj['95']
Sign up to request clarification or add additional context in comments.

9 Comments

I tried but it didn't worked. is it possible to delete an element of object on which we running, $.each()?
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]];
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.
@Shubham, that works.
delete is a native Javascript function. A jquery object is not 100% compatible to a plain Javascript object
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.