如何使用lodash链接功能? [英] How do you chain functions using lodash?
问题描述
我有一个看起来像的对象
I have an object that looks like
var foundUser = {
charData: []
}
然后我使用mysql从数据库中加载对象,然后调用
which then I load an object from a database using mysql and then I call
_.assignIn(foundUser, rows[0]);
但是我得到了一些我不需要的额外属性(使用select无法解决此问题)
But I get a few extra properties that I don't need (this isn't solvable by using select)
所以我打电话
foundUser = _.omit(foundUser, ['blah']);
但是如果我能做的话会很好
But it would be nice if I could just do
_.assignIn(foundUser, rows[0]).omit(rows[0], ['blah']);
但这会引发错误,是我做错了还是有其他方法可以完成?
But that throws an error, am I doing it wrong or is there another way this can be done?
推荐答案
要与lodash链接,首先必须包装对象:
To chain with lodash, you first have to wrap the object:
_(foundUser).assignIn(rows[0]).omit(['blah']).value();
进一步澄清:
_
创建一个lodash对象,该对象允许隐式方法链接.隐式方法链接意味着在某些情况下它可能返回一个原始值,在其他情况下,它可能会返回一个lodash对象,您需要通过调用.value()
对其进行拆包.
The _
creates a lodash object which allows for implicit method chaining. Implicit method chaining means that in certain circumstances it may return a primitive value, in others it may return a lodash object that you need to unwrap by calling .value()
on it.
如果使用_.chain(...)
,则将使用显式方法链创建lodash对象.结果始终是包装的值,并且始终需要通过在其上调用.value()
来对其进行包装.
If you'd use _.chain(...)
, you'd be creating a lodash object with explicit method chaining. The result is always a wrapped value and always needs to be unwrapped by calling .value()
on it.
要获取更多参考,请参见文档链接:
For further reference here are the links to the documentation:
这篇关于如何使用lodash链接功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!