user1486133
user1486133

Reputation: 1487

Removing a nested object using underscore.js _.omit

I have a nested object e.g:

data.object = [Object],[Object],[Object],[Object]

I want to completely remove the 3rd Object, so data.object[2] should no longer exist and I should see:

data.object = [Object],[Object],[Object]

Using delete retains the 3rd object but as undefined:

data.object = [Object],[Object],undefined,[Object]

I understand that _.omit can be used in a similar way but my attempts do not work:

_.omit(data.object,data.object[2])

Upvotes: 0

Views: 470

Answers (1)

zerkms
zerkms

Reputation: 254944

No need to use underscore or any other third party library

data.object.splice(2, 1);

There is a standard JS function for that. It mutates the given array in-place, and removes the 1 element at the index 2.

References:

Upvotes: 2

Related Questions