使用 Lodash 链和过滤器不返回过滤值

Using Lodash chain and filter not returning filtered value

我正在尝试使用 LoDash 过滤键 state === accepted 所在的对象。 quoteList 数组中有两个对象。 console.log 是 returning 数组中的 2 个对象;我期待它 return 1 个对象,其中 state === accepted。我正在使用 chain,因为一旦我开始工作,我将需要使用其他功能。

let quoteList = shipment.quotes
const QuoteSlides = _
.chain(quoteList)
.filter(['state', 'accepted'])
console.log(quoteList)

虽然您记录的对象不正确(如@Baruch 评论中所述),但您还必须使用 .value() 函数解包结果才能获得实际结果:

const QuoteSlides = _
  .chain(quoteList)
  .filter(['state', 'accepted'])
  .value()

当您在对象上调用 _.chain 时,Lodash 会创建一个包装器。这称为显式链接。 .value() 删除包装器和 returns 实际结果,将分配给 QuoteSlides.

查看更多 implicit and explicit 链序列。