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
Solution 1:
You can approach it from either an "allow list" or a "block list" way:
// Block list// Remove the values you don't wantvar result = _.omit(credentials, ['age']);
// Allow list// Only allow certain valuesvar result = _.pick(credentials, ['fname', 'lname']);
If it's reusable business logic, you can partial it out as well:
// Partial out a "block list" versionvar clean = _.partial(_.omit, _, ['age']);
// and latervar result = clean(credentials);
Note that Lodash 5 will drop support for omit
A similar approach can be achieved without Lodash:
consttransform = (obj, predicate) => {
returnObject.keys(obj).reduce((memo, key) => {
if(predicate(obj[key], key)) {
memo[key] = obj[key]
}
return memo
}, {})
}
constomit = (obj, items) => transform(obj, (value, key) => !items.includes(key))
constpick = (obj, items) => transform(obj, (value, key) => items.includes(key))
// Partials// Lazy cleanconstcleanL = (obj) => omit(obj, ['age'])
// Guarded cleanconstcleanG = (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)
Solution 2:
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);
<scriptsrc="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);
Solution 3:
You can easily do this using _.pick:
var model = {
fname: null,
lname: null
};
var credentials = {
fname: 'abc',
lname: 'xyz',
age: 2
};
var result = _.pick(credentials, _.keys(model));
console.log('result =', result);
<scriptsrc="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>
But you can simply use pure JavaScript (specially if you use ECMAScript 6), like this:
const model = {
fname: null,
lname: null
};
const credentials = {
fname: 'abc',
lname: 'xyz',
age: 2
};
const newModel = {};
Object.keys(model).forEach(key => newModel[key] = credentials[key]);
console.log('newModel =', newModel);
Solution 4:
Lodash unset
is suitable for removing a few unwanted keys.
const myObj = {
keyOne: "hello",
keyTwo: "world"
}
unset(myObj, "keyTwo");
console.log(myObj); /// myObj = { keyOne: "hello" }
Solution 5:
Here I have used omit() for the respective 'key' which you want to remove... by using the Lodash library:
var credentials = [{
fname: "xyz",
lname: "abc",
age: 23
}]
let result = _.map(credentials, object => {
return _.omit(object, ['fname', 'lname'])
})
console.log('result', result)
Post a Comment for "Removing Object Properties With Lodash"