Consider the following code:
circle.each(function (d) {
//...code
});
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
});
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
});
Consider the following code:
circle.each(function (d) {
//...code
});
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
});
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
});
Share
Improve this question
edited Sep 14, 2015 at 14:21
taxicala
asked Sep 14, 2015 at 14:10
taxicalataxicala
21.8k7 gold badges40 silver badges66 bronze badges
1
- i've edited my question in order to avoid confusions. Code is not relevant. everything is working I just want to know how to properly break the loop if its posible. – taxicala Commented Sep 14, 2015 at 14:22
2 Answers
Reset to default 6No, there is not. Take a look at the each source code https://github./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.
you can do following
var flag = false;
circle.some(function (d) {
if (flag) return true;
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745205605a4616578.html
评论列表(0条)