将JavaScript粘贴到控制台上;代码的一部分没有运行

时间:2017-09-10 06:56:12

标签: javascript jquery google-chrome

这是我的代码,它从维基百科中获取英语中最常用的单词,然后收集页面上的所有单词并过滤单词列表中最常见的单词:

// get table data from most common words
var arr = [];
$.ajax({
   url: 'https://en.wikipedia.org/wiki/Most_common_words_in_English',
})
.done(function(html) {
   var table = $(html).find(".wikitable");
   for(var i = 1; i < 101; i++) {
       arr[i - 1] = table[0].rows[i].children[0].innerText;
   }
});

var text = document.getElementById('content').innerText;
var words = text.split(/[^a-zA-Z]/);
//filter empty strings
words = words.filter(Boolean);
//filter single characters
words = words.filter(function(word) {
    return word.length > 1;
});

words = words.filter(function(word) {
    return word !== 'was';
});

words = words.filter(function(word) {
    return word !== 'where';
});

words = words.filter(function(word) {
    return word !== 'is';
});

words = words.filter(function(word) {
    return word !== 'are';
});

// filter stopWords from 100 most common words wikipedia page
words = words.filter(function(word) {
    var isNotStopWord = true;
    var i = 0;
    for(var stopWord in arr) {
        if(word === arr[i++]) {
            isNotStopWord = false;
            break;
        }
    }
    return isNotStopWord;
});

但最后一段代码似乎无法运行:

// filter stopWords from 100 most common words wikipedia page
words = words.filter(function(word) {
    var isNotStopWord = true;
    var i = 0;
    for(var stopWord in arr) {
        if(word === arr[i++]) {
            isNotStopWord = false;
            break;
        }
    }
    return isNotStopWord;
});

除非我将该部分粘贴到控制台上并再次运行它?

2 个答案:

答案 0 :(得分:0)

这部分代码甚至在第一次运行,但是当arr []第一次为空时你可能看不到它的效果。 如果你在ajax done方法中移动那段代码,这段代码应该可行。

  var arr = [];
    $.ajax({
       url: 'https://en.wikipedia.org/wiki/Most_common_words_in_English',
    })
    .done(function(html) {
       var table = $(html).find(".wikitable");
       for(var i = 1; i < 101; i++) {
           arr[i - 1] = table[0].rows[i].children[0].innerText;
       }
     words = words.filter(function (element,index){
         return (arr.indexOf(element) === -1)
     })


    console.log(words)

    });

    var text = document.getElementById('content').innerText;
    var words = text.split(/[^a-zA-Z]/);
    //filter empty strings
    words = words.filter(Boolean);
    //filter single characters
    words = words.filter(function(word) {
        return word.length > 1;
    });

    words = words.filter(function(word) {
        return word !== 'was';
    });

    words = words.filter(function(word) {
        return word !== 'where';
    });

    words = words.filter(function(word) {
        return word !== 'is';
    });

    words = words.filter(function(word) {
        return word !== 'are';
    });

答案 1 :(得分:-1)

您正在混合使用不同的方法来迭代数组的项目。使用......

    for(const stopWord of arr) {
        if(word === stopWord) {
            isNotStopWord = false;
            break;
        }
    }

或......

    for(let i=0; i < arr.length; i++) {
        if(word === arr[i]) {
            isNotStopWord = false;
            break;
        }
    }