admin管理员组文章数量:1341409
I have the following working code written with lodash, latest version
_.forEach(_.filter($ctrl.data, ['x', 'y']), function (elem) {
...
});
If I try to transform this into a chain, it will never execute the block code inside forEach
.
_.chain($ctrl.data)
.filter(['x', 'y'])
.forEach(function (elem) {
...
});
Why?
I have the following working code written with lodash, latest version
_.forEach(_.filter($ctrl.data, ['x', 'y']), function (elem) {
...
});
If I try to transform this into a chain, it will never execute the block code inside forEach
.
_.chain($ctrl.data)
.filter(['x', 'y'])
.forEach(function (elem) {
...
});
Why?
Share Improve this question asked Jan 25, 2017 at 10:32 tzortziktzortzik 5,16310 gold badges60 silver badges95 bronze badges 6-
if you console.log
_.chain($ctrl.data).filter(['x', 'y'])
, what does it return? – Dani Commented Jan 25, 2017 at 10:39 -
I debugged using
_.chain($ctrl.data).filter(['x', 'y']).value()
and it returns the filtered array, as expected. – tzortzik Commented Jan 25, 2017 at 10:40 -
then you should try
_.chain($ctrl.data).filter(['x', 'y']).value().forEach(function(elem) { .. });
– Dani Commented Jan 25, 2017 at 10:43 -
This is not ok. It will call
forEach
on javascriptArray
which is not right. My code should be backwards patile and there are some older browsers which does not supportforEach
. – tzortzik Commented Jan 25, 2017 at 10:45 - 1 _.chain([1,2,3]).forEach(console.log) works as expected, your code should too. – Stefan Dragnev Commented Jan 25, 2017 at 10:47
3 Answers
Reset to default 4I fixed the problem by using _($ctrl.data)
instead of _.chain($ctrl.data)
. I read that the main difference here is that the global function from lodash calls _.value()
when it has a "terminator function" at the end.
An equivalent using _.chain
would be to explicitly call _.value()
at before _.forEach()
. But still can't figure out what is the problem because, from my point of view, a forEach
is a "terminator function".
This happens because execution of chained methods in lodash is lazy.
From lodash documentation on sequences:
The execution of chained methods is lazy, that is, it's deferred until _#value is implicitly or explicitly called.
That means that following code:
_.chain(['a','b','c'])
.filter(_.stubTrue)
.forEach(i => console.log(i))
will not print anything to console unless the sequence will be ended with value()
:
_.chain(['a','b','c'])
.filter(_.stubTrue)
.forEach(i => console.log(i))
.value()
a
b
c
I just ran into a similar issue. I was using the _(blah)
syntax and I was still running into issues.
In my case, the code looked like this:
_(items).forEach((x) => doSomething(x));
It turns out the doSomething
was returning a boolean
. I guess lodash sees this return value and treats forEach
the same as map
, not evaluating it. However, I tried tacking on a value()
onto the end and I get a value is not a function
error at runtime.
In my case, the problem was solved by wrapping the doSomething
call:
_(items).forEach((x) => { doSomething(x); });
Seems more like a bug than anything.
本文标签: javascriptlodashchaining filter and forEachStack Overflow
版权声明:本文标题:javascript - lodash - chaining filter and forEach - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743672639a2519787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论