Remove key value pair from an object in javascript using underscoreJs

114 views Asked by At

I have an array
var data = {email: '[email protected]', phone: '3434344332', firstName: 'john', lastName: 'Doe'};

How do i remove one or more elements from the array?

I Tried _.without( data, data.email ) , this removes the email field but also changes the Object to array without keys.

result ["3434344332", "john", "Doe"]

What I want to achieve {phone: '3434344332', firstName: 'john', lastName: 'Doe'}

2

There are 2 answers

0
mochaccino On BEST ANSWER

without only works with arrays. If you pass an object, it'll get turned into an array in a similar manner to Object.values. You should be using omit instead:

_.omit(data, "email")

reference

0
zavg On

Probably you can just use the vanilla JS

delete data.email

However, this solution modifies the original object (does not make a copy of object with an excluded field).