罗达什 |搜索对象的关键

Lodash | Search object for key

我正在尝试在我拥有的对象中搜索与输入名称匹配的键。这样我就可以赋值了。

<input type="text" name="first_name"/>

const inputs = document.querySelectorAll('inputs');
const obj = {
    first_name: "Bob",
    last_name: "Marley"
};

我想将键 first_name 匹配到名称为 first_name 的输入上。

我已经达到:

_.forEach(inputs, (input) => {
    const value = _.filter(obj, input.name);
    input.value = value;
});

但是,我一直未定义,所以我不认为过滤器是正确使用的函数。但是,我查看了文档,并没有真正看到任何接近的东西。我曾尝试转换为数组,但也没有希望,因为所有键都转到了索引号。

我想 _.get(obj, input.name) 就是您要找的。

这里是文档:

_.get(object, path, [defaultValue])

Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.

示例:

const obj = {'a':1, 'b':{'c': 2}}
_.get(obj, 'a')   // returns 1
_.get(obj, 'b.c') // returns 2