最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Break D3 Each Loop Without a flag - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 6

No, 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;
});
发布评论

评论列表(0)

  1. 暂无评论