最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to specify default values for null in Lodash or Underscore? - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question asked Apr 1, 2017 at 13:14 JohnnyQJohnnyQ 5,1196 gold badges52 silver badges65 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

Thanks 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>

发布评论

评论列表(0)

  1. 暂无评论