lodash表示“按对象路径选择"? [英] lodash for "select by object path"?

查看:40
本文介绍了lodash表示“按对象路径选择"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个对象(或这些对象的数组):

Let's say I have this object (or an array of these objects):

var person = {
    birth: {
        place: {
            country: 'USA'
        }
    }
};

我认为我有一个lodash函数,可以传入'birth.place.country'并返回值USA.

I thought there was a lodash function where I could pass in 'birth.place.country' and get back the value USA.

lodasdh 3.x中是否有这样的功能,或者我想像这个吗?

Is there such a function in lodasdh 3.x, or am I Imagining this?

推荐答案

您可以使用_.get函数:

_.get(person, 'birth.place.country', 'optionalDefaultValue');

lodash还提供了一个名为_.result的函数,该函数也可以调用函数.

lodash also provides a function called _.result that can also call functions.

这篇关于lodash表示“按对象路径选择"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆