简体   繁体   English

为什么第二个数组保持为空?

[英]Why does the second array remain empty?

The goal of this "counter" is to see how many different words are inside the "ToCount"string. 此“计数器”的目标是查看“ ToCount”字符串中有多少个不同的单词。 To do that it makes ToCount an array and then iterates over the elements, checking if they already are there and if not, adding them there. 为此,它使ToCount成为数组,然后遍历元素,检查它们是否已经存在,如果不存在,则将它们添加到那里。

The ToCountArr2 remains empty after the loop and a length of 0 is being displayed. 循环后,ToCountArr2保持为空,长度为0。 Why does that happen and what can I do about it? 为什么会发生这种情况,我该怎么办?

I ran a debugger and saw that no elements are added to the second list, as if nothing appenned inside the "if" control if the i-th element of the first array already is inside the second array. 我运行了一个调试器,发现没有元素添加到第二个列表中,好像如果第一个数组的第i个元素已经在第二个数组中,则“ if”控件中没有任何内容。

function counter(){
    var ToCount = document.getElementById("demo").value; //the contents of a textbox
    var ToCountArr1 = ToCount.split(" ");
    var ToCountArr2 = new Array;
    var i = 0;
    var lengthToCountArr1 = ToCountArr1.length;
    var wordToPush;
    while (i < lengthToCountArr1){
    if(ToCountArr2.includes(ToCountArr1[i] === false)) {
            wordToPush = ToCountArr1[i];
            ToCountArr2.push(wordToPush);
        }
        i = i + 1;
    }
    alert(ToCountArr2.length);
}

The issue is with this line if(ToCountArr2.includes(ToCountArr1[i] === false)) . 问题在于这一行if(ToCountArr2.includes(ToCountArr1[i] === false)) Here the braces need to be after ToCountArr1[i] , where as this line ToCountArr1[i] === false) is checking whether that value in ToCountArr1 is true or false. 在这里,花括号必须ToCountArr1[i] ,其中,这行ToCountArr1[i] === false)正在检查ToCountArr1中的值是true还是false。

This line 这条线

if(ToCountArr2.includes(ToCountArr1[i] === false)) will be evaluated as if(ToCountArr2.includes(ToCountArr1[i] === false))将被评估为

if(ToCountArr2.includes(true/false)) depending on result of ToCountArr1[i] === false) if(ToCountArr2.includes(true/false))取决于ToCountArr1[i] === false)

 function counter() { var ToCount = document.getElementById("demo").value; //the contents of a textbox var ToCountArr1 = ToCount.split(" "); var ToCountArr2 = new Array; var i = 0; var lengthToCountArr1 = ToCountArr1.length; var wordToPush; while (i < lengthToCountArr1) { if (ToCountArr2.includes(ToCountArr1[i]) === false) { wordToPush = ToCountArr1[i]; ToCountArr2.push(wordToPush); } i = i + 1; } console.log(ToCountArr2.length); } counter() 
 <input type='text' id='demo' value='Test Values'> 

You can minimize if (ToCountArr2.includes(ToCountArr1[i]) === false) { by replacing it with 您可以将if (ToCountArr2.includes(ToCountArr1[i]) === false) {

if (!ToCountArr2.includes(ToCountArr1[i])) {

Your wordcount function should use a parameter so you can pass a string in. This means you can use the wordcount function on an any string, not just the "demo" element. 您的wordcount函数应该使用一个参数,以便您可以传入字符串。这意味着您可以在任何字符串上使用wordcount函数,而不仅仅是“ demo”元素。 Also, this is a good time to learn about Map - 另外,这是学习Map的好时机-

 const wordcount = (str = "") => { const result = new Map for (const s of str.split(/ /)) if (s === "") continue else if (result.has(s)) result.set(s, result.get(s) + 1) else result.set(s, 1) return Array.from(result.entries()) } const prettyPrint = (value) => console.log(JSON.stringify(value)) 
 <!-- pass this.value as the string for wordcount -- wordcount returns a value that we could use elsewhere -- prettyPrint displays the value to the console --> <input onkeyup="prettyPrint(wordcount(this.value))"> 

Run the code snippet and copy/paste the following line into the field - 运行代码段并将以下行复制/粘贴到字段中-

this is the captain speaking. is this the commander?

You will see this output - 您将看到此输出-

[["this",2],["is",2],["the",2],["captain",1],["speaking.",1],["commander?",1]]

Here is an working example. 这是一个工作示例。 I think it will help you right way. 我认为这会帮助您正确的方法。 Here I use indexOf to check the value exist on a array. 在这里,我使用indexOf来检查数组中是否存在该值。

function counter(){
    var ToCount = "I am string just for text.";//document.getElementById("demo").value; //the contents of a textbox
    var ToCountArr1 = ToCount.split(" ");
    var ToCountArr2 = new Array;
    var i = 0;
    var lengthToCountArr1 = ToCountArr1.length;
    var wordToPush;
    while (i < lengthToCountArr1){
    if( ToCountArr2.indexOf(ToCountArr1[i]) == -1 ) {
            wordToPush = ToCountArr1[i];
            ToCountArr2.push(wordToPush);
        }
        i = i + 1;
    }
    alert(ToCountArr2.length);
}
counter();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM