簡體   English   中英

Cheerio每個循環不會盡早爆發

[英]Cheerio each loop does not break out early

在Cheerio / Jquery 文檔中指出,返回false應該盡早中斷每個循環。

我有以下代碼:

"use strict";
let cheerio = require('cheerio');
let $ = cheerio.load('<a href="www.test.com"></a><a href="www.test.com"></a>');

$('a').each(function(index,value){
  console.log(index);
  return false;
});

在我的腦海中,它應該打印0到控制台,但是它打印0和1。我缺少什么?

代碼很好。 如果查看cheerio的源代碼,可以看到如果返回false,則循環不會繼續。

https://github.com/cheeriojs/cheerio/blob/master/lib/api/traversing.js#L298

exports.each = function(fn) {
  var i = 0, len = this.length;
  while (i < len && fn.call(this[i], i, this[i]) !== false) ++i;
  return this;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM