考虑:
var result = _.chain(foo)
.stuff()
.morestuff()
.extend(extraKeysValues) // <-- problem is here
.value();
它的作用是使用 extraKeysValues
中的内容扩展链接数据集。这可能会覆盖数据。
当在链
中使用时,我想要相反的行为 - 我想将链数据添加到extraKeysValues
,这样我就不会覆盖任何数据。
那么我怎样才能有效地颠倒_.extend(destination, *sources)
的顺序呢?当在链内使用时?我意识到这将是一个多步骤的过程,但我不想打破这个链条。
最佳答案
我认为defaults(object, *defaults)
方法做你正在寻找的事情。
Fill in undefined properties in object with the first value present in the following list of defaults objects.
var result = _.chain(foo)
.stuff()
.morestuff()
.defaults(extraKeysValues)
.value();
这样,result
中的数据就不会被extraKeysValues
数据覆盖。
关于javascript - 在链中使用时,下划线的 `extend` 的顺序相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434253/