Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing object properties with Lodash

I have to remove unwanted object properties that do not match my model. How can I achieve it with Lodash?

My model is:

var model = {    fname: null,    lname: null } 

My controller output before sending to the server will be:

var credentials = {     fname: "xyz",     lname: "abc",     age: 23 } 

I am aware I can use

delete credentials.age 

but what if I have lots of unwanted properties? Can I achieve it with Lodash?

like image 614
Alaksandar Jesus Gene Avatar asked Oct 30 '16 13:10

Alaksandar Jesus Gene


People also ask

How do I remove a property from object Lodash?

The Lodash _. unset() method is used to remove the property at the path of the object. If the property is removed then it returns True value otherwise, it returns False.

How do I remove a property from an array of objects?

To remove a property from all objects in an array:Use the Array. forEach() method to iterate over the array. On each iteration, use the delete operator to delete the specific property. The property will get removed from all objects in the array.

What does Lodash omit do?

Lodash helps in working with arrays, strings, objects, numbers, etc. The _. omit() method is used to return a copy of the object that composed of the own and inherited enumerable property paths of the given object that are not omitted.

What is Lodash remove?

remove() method in Lodash removes all the elements from an array that returns a truthy value for the specified predicate. This method will mutate the original array and return an array of all the removed elements.


2 Answers

You can approach it from either an "allow list" or a "block list" way:

// Block list // Remove the values you don't want var result = _.omit(credentials, ['age']);  // Allow list // Only allow certain values var result = _.pick(credentials, ['fname', 'lname']); 

If it's reusable business logic, you can partial it out as well:

// Partial out a "block list" version var clean = _.partial(_.omit, _, ['age']);  // and later var result = clean(credentials); 

Note that Lodash 5 will drop support for omit

A similar approach can be achieved without Lodash:

const transform = (obj, predicate) => {     return Object.keys(obj).reduce((memo, key) => {     if(predicate(obj[key], key)) {         memo[key] = obj[key]     }     return memo     }, {}) }  const omit = (obj, items) => transform(obj, (value, key) => !items.includes(key))  const pick = (obj, items) => transform(obj, (value, key) => items.includes(key))  // Partials // Lazy clean const cleanL = (obj) => omit(obj, ['age'])  // Guarded clean const cleanG = (obj) => pick(obj, ['fname', 'lname'])   // "App" const credentials = {     fname:"xyz",     lname:"abc",     age:23 }  const omitted = omit(credentials, ['age']) const picked = pick(credentials, ['age']) const cleanedL = cleanL(credentials) const cleanedG = cleanG(credentials) 
like image 145
Chris Avatar answered Oct 08 '22 13:10

Chris


Get a list of properties from model using _.keys(), and use _.pick() to extract the properties from credentials to a new object:

var model = {    fname:null,    lname:null };  var credentials = {     fname:"xyz",     lname:"abc",     age:23 };  var result = _.pick(credentials, _.keys(model));  console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

If you don't want to use Lodash, you can use Object.keys(), and Array.prototype.reduce():

var model = {    fname:null,    lname:null };  var credentials = {     fname:"xyz",     lname:"abc",     age:23 };  var result = Object.keys(model).reduce(function(obj, key) {   obj[key] = credentials[key];   return obj; }, {});  console.log(result);
like image 36
Ori Drori Avatar answered Oct 08 '22 14:10

Ori Drori