中断D3每个循环没有标志 [英] Break D3 Each Loop Without a flag
问题描述
请考虑以下代码:
circle.each(function (d) {
//...code
});
如何打破循环?有没有一个自然的D3方式来打破每个循环?我的意思是没有如下的标志:
How can I break the loop? Is there a natural D3 way to break out of an each loop? I mean without a flag as follows:
var flag = false;
circle.each(function (d) {
if (flag) return;
if (someCondition) flag = true;
//...code
});
我试过在if语句中返回false但是没有工作(想想也许这会工作方式与 jquery.each
相同,但我错了):
I've tried returning false inside the if statement but it did not work (thought that maybe this would work the same as jquery.each
but I was wrong):
circle.each(function (d) {
if (someCondition) return false; //Not working
//...code
});
推荐答案
不,请查看每个源代码 https://github.com /mbostock/d3/blob/78e0a4bb81a6565bf61e3ef1b898ef8377478766/src/selection/each.js 。
No, there is not. Take a look at the each source code https://github.com/mbostock/d3/blob/78e0a4bb81a6565bf61e3ef1b898ef8377478766/src/selection/each.js.
您可以抛出异常来打破循环,但是除非你的情况真的是异常,使用异常可能比帮助更混乱。
You may be able to throw an exception to break the loop, but unless your case is really "exceptional", using an exception is probably more confusing than helpful.
这篇关于中断D3每个循环没有标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!