为什么我的数组是'undefined'? (香草 javascript)
Why is my array 'undefined'? (vanilla javascript)
我正在尝试使用 javascript 制作一个简单的 'bad words' 过滤器。它的意思是监听页面上的任何提交事件,然后遍历所有文本类型的输入字段,通过将输入的文本与单词列表进行比较来检查它们是否有错误内容,最后 return 一个相应的控制台。log/alert(现在)。
我有两个文件:word-list.js
包含关键字(首先加载)和 filter.js
,它从 word-list.js
.
中提取包含所有单词的数组
我的问题是,swear_words_arr[1]
是“undefined
”,我不明白为什么。我一直在寻找解决方案,但我似乎仍然无法确定造成这种情况的原因。非常感谢帮助。
// get all inputs type = text and turn html collection into array
var getInputs = document.querySelectorAll("input[type=text]")
var inputs = Array.from(getInputs);
//var swear_alert_arr -> from in word-list.js
var swear_alert_arr = new Array();
var swear_alert_count = 0;
function reset_alert_count() {
swear_alert_count = 0;
}
function validate_text() {
reset_alert_count();
inputs.forEach(function(input) {
var compare_text = input.value;
console.log(compare_text);
for (var i = 0; i < swear_words_arr.length; i++) {
for (var j = 0; j < compare_text.length; i++) {
if (
swear_words_arr[i] ==
compare_text.substring(j, j + swear_words_arr[i].length).toLowerCase()
) {
swear_alert_arr[swear_alert_count] =
compare_text.substring(
j,
j + swear_words_arr[i].length
);
swear_alert_count++;
}
}
}
var alert_text = "";
for (var k = 1; k <= swear_alert_count; k++) {
alert_text += "\n" + "(" + k + ") " + swear_alert_arr[k - 1];
if (swear_alert_count > 0) {
alert("No!");
console.log('omg no bad stuff! D:');
} else {
console.log('no bad stuff found :)');
}
}
});
}
window.onload = reset_alert_count;
window.addEventListener('submit', function() {
validate_text();
});
您似乎没有声明要访问的数组。
但是,不是使用嵌套循环和跟踪循环计数器的循环,而是获取一个新数组,其中包含提交数组中的任何坏词。您可以通过多种方式执行此操作,但 Array.prototype.filter() 方法效果很好:
let badWords = ["worse", "terrible", "horrible", "bad"];
let submittedWords = ["Good", "Terrible", "Great", "Fabulous", "Bad", "OK"];
// Loop over the submitted words and return an array of all the bad words found within it
let bad = submittedWords.filter(function(word){
// Do a case-insensitive match test. Return the word from the submitted words
// if it's on the bad word list.
return badWords.indexOf(word.toLowerCase()) > -1 ? word: null;
});
console.log("Bad words found in submitted data: " + bad.join(", "));
我正在尝试使用 javascript 制作一个简单的 'bad words' 过滤器。它的意思是监听页面上的任何提交事件,然后遍历所有文本类型的输入字段,通过将输入的文本与单词列表进行比较来检查它们是否有错误内容,最后 return 一个相应的控制台。log/alert(现在)。
我有两个文件:word-list.js
包含关键字(首先加载)和 filter.js
,它从 word-list.js
.
我的问题是,swear_words_arr[1]
是“undefined
”,我不明白为什么。我一直在寻找解决方案,但我似乎仍然无法确定造成这种情况的原因。非常感谢帮助。
// get all inputs type = text and turn html collection into array
var getInputs = document.querySelectorAll("input[type=text]")
var inputs = Array.from(getInputs);
//var swear_alert_arr -> from in word-list.js
var swear_alert_arr = new Array();
var swear_alert_count = 0;
function reset_alert_count() {
swear_alert_count = 0;
}
function validate_text() {
reset_alert_count();
inputs.forEach(function(input) {
var compare_text = input.value;
console.log(compare_text);
for (var i = 0; i < swear_words_arr.length; i++) {
for (var j = 0; j < compare_text.length; i++) {
if (
swear_words_arr[i] ==
compare_text.substring(j, j + swear_words_arr[i].length).toLowerCase()
) {
swear_alert_arr[swear_alert_count] =
compare_text.substring(
j,
j + swear_words_arr[i].length
);
swear_alert_count++;
}
}
}
var alert_text = "";
for (var k = 1; k <= swear_alert_count; k++) {
alert_text += "\n" + "(" + k + ") " + swear_alert_arr[k - 1];
if (swear_alert_count > 0) {
alert("No!");
console.log('omg no bad stuff! D:');
} else {
console.log('no bad stuff found :)');
}
}
});
}
window.onload = reset_alert_count;
window.addEventListener('submit', function() {
validate_text();
});
您似乎没有声明要访问的数组。
但是,不是使用嵌套循环和跟踪循环计数器的循环,而是获取一个新数组,其中包含提交数组中的任何坏词。您可以通过多种方式执行此操作,但 Array.prototype.filter() 方法效果很好:
let badWords = ["worse", "terrible", "horrible", "bad"];
let submittedWords = ["Good", "Terrible", "Great", "Fabulous", "Bad", "OK"];
// Loop over the submitted words and return an array of all the bad words found within it
let bad = submittedWords.filter(function(word){
// Do a case-insensitive match test. Return the word from the submitted words
// if it's on the bad word list.
return badWords.indexOf(word.toLowerCase()) > -1 ? word: null;
});
console.log("Bad words found in submitted data: " + bad.join(", "));