阅读背景:

我在javascript中创建了一个递归函数,用于过滤掉输入数组中的字符串。为什么它会额外经过for循环3次?

来源:互联网 

function filter_list(l) {

  var count = l.length;
  console.log('the count is:', count);

  function loop() {
    for (var i = 0; i < count; i++) {
      var type = (typeof l[i]);

      if (type == 'string') {
        l.splice(i, 1);
        console.log('the list is', l);
        filter_list(l);
      } else {
        console.log('this item is', l[i], 'the list is', l);
      }
    }
  }

  loop()
}

filter_list([1, 2, 'a', 'c', 'd']);function filter_list(l) {

  var co



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: