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?
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.
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.
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.
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.
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);
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)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With