Javascript 条件反转()与链方法(使用Lodash)

Javascript 条件反转()与链方法(使用Lodash),javascript,lodash,Javascript,Lodash,我有以下代码: if (this.config.order === 'asc' ) { return _.chain(this.rows) .sortBy(this.config.orderBy) .slice((this.config.page - 1) * this.config.numOfRows, this.config.page * this.config.numOfRows - 1) .value() } else { return _

我有以下代码:

if (this.config.order === 'asc' ) {
  return _.chain(this.rows)
    .sortBy(this.config.orderBy)
    .slice((this.config.page - 1) * this.config.numOfRows, this.config.page 
    * this.config.numOfRows - 1)
    .value()

} else {
    return _.chain(this.rows)
    .sortBy(this.config.orderBy)
    .reverse()
    .slice((this.config.page - 1) * this.config.numOfRows, this.config.page 
     * this.config.numOfRows - 1)
    .value()
}
这是一个很大的重复,因为条件决定是否使用
reverse()


是否有任何方法可以将其减少为仅调用一个
\uu.chain
块?

这应该可以通过使用来实现,如下所示:

_.chain(this.rows)
.sortBy(this.config.orderBy)
.tap(this.config.order === 'asc' ? _.noop : _.reverse)
// continue your chain
但也许您可以使用u.orderBy而不是u.sortBy,因为前者允许您将排序顺序指定为参数。

Read。它可能会给你的问题提供线索