How do you chain functions using lodash?

前端 未结 4 1600
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 19:36

I have an object that looks like

var foundUser = {
    charData: []
}

which then I load an object from a database using mysql and then I ca

4条回答
  •  执笔经年
    2020-12-23 20:10

    To chain with lodash, you first have to wrap the object:

    _(foundUser).assignIn(rows[0]).omit(['blah']).value();
    

    Further clarification:

    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.

    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:

    Explicit chaining in Lodash

    Implicit chaining in Lodash

提交回复
热议问题