I know the _.defaults
function works to put default values for an object with keys that are undefined
. However it doesn't work with keys that are null
. Is there way to achieve this?
I know the _.defaults
function works to put default values for an object with keys that are undefined
. However it doesn't work with keys that are null
. Is there way to achieve this?
2 Answers
Reset to default 11Thanks to @Jacque for the explanation about null
values. However due to unfortunate inherited code, the object I'm working on returns null
values even though it's intended for undefined
. Here's my approach in achieving this in a more declarative way by omitting the null
properties which will result into undefined
, as a result will create defaults for its values.
const alphabet = {
a: 'A is for apple',
// b: undefined,
c: 'C is for cake',
d: null,
}
const nonNulls = _.omitBy(alphabet, _.isNull) // Omitting null values.
const longer = _.defaults(nonNulls, {
b: 'B is for boy',
})
console.info('Longer Way', longer)
// Or even shorter
const shorter = _.defaults(_.omitBy(alphabet, _.isNull), {
b: 'B is for boy',
})
console.info('Shorter Way', shorter)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
The reason behind this behavior for the _.defaults
helper is that undefined
means a property hasn't been set and null
conveys the meaning of being explicitly set to refer to nothing.
If you still think your variables should hold null
and be set with a function, then you can use _.defaultTo
, a function that returns its first argument normally and its second if the first one was NaN
, null
or undefined
. You'll need to make it loop over all the object's keys yourself though.
_.forOwn(myObject, function (value, key) {
myObject[key] = _.defaultTo(value, defaultValues[key]);
});
However, this won't set values for variables that are not even defined on the original element, since we're looping through its own properties. Plus it's not very efficient. If that's what you wanted, might as well loop on the defaults instead:
_.forOwn(defaultValues, function (value, key) {
if (myObject[key] === undefined || myObject[key] === null)
myObject[key] = value;
});
function getSampleObject() {
return {
a: null,
b: NaN,
// c: undefined,
d: undefined,
e: 'original'
}};
var defaultValues = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5
};
var myObject = getSampleObject();
_.forOwn(myObject, function (value, key) {
myObject[key] = _.defaultTo(value, defaultValues[key]);
});
console.log('defaultTo', myObject);
var myObject = getSampleObject();
_.defaults(myObject, defaultValues);
console.log('defaults', myObject);
var myObject = getSampleObject();
_.forOwn(defaultValues, function (value, key) {
if (myObject[key] === undefined || myObject[key] === null)
myObject[key] = value;
});
console.log('handmade', myObject);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>