我可以使用另一个_.get作为lodash _.get的默认值吗?

时间:2017-06-27 20:09:55

标签: javascript dictionary key lodash default

d = {color:“red”,身高:“5”,年龄:“200”,形状:“square”}

给定一个字典,我可以用Lodash的_.get得到一个值,如:

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

但是,我可以将默认值设置为另一个_.get吗? 像这样:

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

感谢您的任何见解!

1 个答案:

答案 0 :(得分:1)

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"