javascript - 比较我的数组直到数组中有多个值

标签 javascript arrays dynamic-arrays

我正在尝试将一个包含多个值的数组传递给一个函数,该函数将检查这些值是否存在于 div 列表中,如果存在,则用红色背景标记它们。

当我将任意字符串作为参数传递时,我的代码可以正常工作,并且当我的数组中只有一个值时,它也可以正常工作。但是,一旦我的数组中有两个或更多值,它似乎就会崩溃,并且控制台中没有任何信息可以提示我。

我认为问题在于我如何编写比较函数,但也可能在于我如何传递数组。

var postList = document.getElementsByClassName("post");
var userList = new Array();

//compares the user's keyword entries to the text in the divs, and marks "matching" divs with a red background
function listComparison(collection, searchText) {
    for (var i = 0; i < collection.length; i++) {
        if (collection[i].innerText.toLowerCase().indexOf(searchText) > -1) {
            collection[i].style.backgroundColor = "red";
        }
    }
}

//adds user entries to an array on click and clears out the textarea
document.getElementById("save").addEventListener("click", function() {
    var listEntry = document.getElementById("listEntries").value;
    userList.push(listEntry);
    document.getElementById("listEntries").value = "";
    console.log(userList);
})

//event listener for the button that runs the collectionContains function above
document.getElementById("run").addEventListener("click", function() {
    listComparison(postList, userList);
});
div {
    background: #d0dbe6;
    margin: 5px;
    width: 50%;
}

#list {
    width: 300px;
    height: 100px;
}
<textarea placeholder="Enter words here one at a time and click 'Add to Filter'" id="listEntries"></textarea>
<br>
<button id="save" for="listEntries">Add to Filter</button>
<button id="run">Run Filter</button>

<div class="post">religion</div>
<div class="post">cats</div>
<div class="post">hotdogs</div>
<div class="post">babies</div>
<div class="post">test me please</div>
<div class="post">filler</div>
<div class="post">lorem ipsum</div>
<div class="post">your mom</div>
<div class="post">religion again</div>
<div class="post">build a wall</div>
<div class="post">no it's becky</div>
<div class="post">anything with religions in it is screwed!</div>

最佳答案

问题在于您将数组传递给 String#indexOf()。 (如 collection[i].innerText.toLowerCase().indexOf(searchText))。该函数需要一个字符串作为搜索词,而不是一个数组。

发生的事情是您的数组正在转换为字符串。当您的数组只包含一个字符串而没有其他元素时,它可以工作,因为它的字符串表示是同一个字符串。但是当你的数组包含多个元素时,它的字符串表示是所有这些字符串的逗号分隔列表,并且不会正确比较。

您将需要遍历数组以便为数组中的每个字符串搜索集合中的项目(我已重命名参数以明确您传递的是数组):

var postList = document.getElementsByClassName("post");
var userList = new Array();

//compares the user's keyword entries to the text in the divs, and marks "matching" divs with a red background
function listComparison(collection, searchList) {
    for (var i = 0; i < searchList.length; i++) {
        for (var j = 0; j < collection.length; j++) {
            if (collection[j].innerText.toLowerCase().indexOf(searchList[i]) > -1) {
                collection[j].style.backgroundColor = "red";
            }
        }
    }
}

//adds user entries to an array on click and clears out the textarea
document.getElementById("save").addEventListener("click", function() {
    var listEntry = document.getElementById("listEntries").value;
    userList.push(listEntry);
    document.getElementById("listEntries").value = "";
    console.log(userList);
})

//event listener for the button that runs the collectionContains function above
document.getElementById("run").addEventListener("click", function() {
    listComparison(postList, userList);
});
div {
    background: #d0dbe6;
    margin: 5px;
    width: 50%;
}

#list {
    width: 300px;
    height: 100px;
}
<textarea placeholder="Enter words here one at a time and click 'Add to Filter'" id="listEntries"></textarea>
<br>
<button id="save" for="listEntries">Add to Filter</button>
<button id="run">Run Filter</button>

<div class="post">religion</div>
<div class="post">cats</div>
<div class="post">hotdogs</div>
<div class="post">babies</div>
<div class="post">test me please</div>
<div class="post">filler</div>
<div class="post">lorem ipsum</div>
<div class="post">your mom</div>
<div class="post">religion again</div>
<div class="post">build a wall</div>
<div class="post">no it's becky</div>
<div class="post">anything with religions in it is screwed!</div>

关于javascript - 比较我的数组直到数组中有多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626926/

相关文章:

javascript - JavaScript获取IE浏览器语言

javascript - 单击时循环数组

javascript - 按输入类型对数组和分组进行排序

javascript - 如何在动态添加的数组中具有不同的值

java - 使用数组实现 Set 类

c++ - 为什么我的指针输出一个字符串而不是 C++ 中的内存地址?

javascript - Jquery 动态更改 CSS 主题 - 强制更新类?

javascript - 复制第一个属性的值,然后使用第一个属性的值创建新属性?

javascript - 如何检测div中的单个单词

php - 使用两个不同的数组构建一个 MySQL INSERT,每个数组使用