Search code examples
javascriptdictionarykeylodashdefault

Can I use another _.get as the default for lodash _.get?


d = {color:"red",height:"5",age:"200",shape:"square"}

given a dictionary, I can get a value with Lodash's _.get like so:

var myval = _.get(d, "height",null);

But, can I set the default to another _.get? Like this:

var myval = _.get(d, "length",_.get(d, "height",null));

Thanks for any insight!


Solution

  • Yes.

    d = { color: "red", height: "5", age: "200", shape: "square" }
    // Object { color: "red", height: "5", age: "200", shape: "square" }
    
    _.get(d, 'hello', _.get(d, 'height', null));
    // "5"