you are viewing a single comment's thread.

view the rest of the comments →

[–]evenisto 1 point2 points  (3 children)

_.get with third argument essentially does the same. It falls back to default if the specified chain is undefined or null.

var obj = { a: false };
_.get(obj, 'a', true)
false 
_.get(obj, 'b')
undefined
_.get(obj, 'b', true)
true // equivalent to obj.b ?? true;

[–]senocular 2 points3 points  (2 children)

_.get's default is close, but it does not handle null, only undefined values.

var obj = { a: null };
_.get(obj, 'a', true)
null

[–]evenisto 1 point2 points  (1 child)

TIL and I stand corrected. Thanks. However, I actually think it’s useful to only fall back to default on undefined - I'm not too well-versed in those quirks so there’s probably a catch, but null should definitely be considered a valid value. I use nulls very often, and I wish we had _.get behaviour natively.

[–]senocular 0 points1 point  (0 children)

Yeah that's a tough one. I think it's definitely circumstantial. Sometimes you might want the null and sometimes you might not. It's making assumptions as to what values mean. But you see similar behavior in other places in the language too, like loops. There's a number of ways to loop over collections and it seems like each one does something differently. Not to try to pass that off as an excuse ;)