gpt4 book ai didi

javascript - 对数组进行字符串搜索不会返回任何结果

转载 作者:行者123 更新时间:2023-12-03 09:09:22 24 4
gpt4 key购买 nike

我有一个公共(public)信息数据库,我想使用 jQuery、JavaScript(以及后来的 HTML 和 CSS)重新格式化该信息。我正在尝试使用提供的信息在表中创建新组。在这个特定的示例中,我想找到表中出现的八月的每个实例。

我尝试了两种方法来搜索内容,并且都返回一个空数组( '[]' )。这很令人困惑,因为我可以在控制台中记录内容,但它不会添加到新数组中。

//DETECT ROWS, COLUMNS
var tableArray = [];

$("table#tableTest tr").each(function() {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function() { arrayOfThisRow.push($(this).text()); });
tableArray.push(arrayOfThisRow);
}
});

//SAMPLE LOGS
console.log("R1, C5: " + tableArray[4][3]);
console.log("Total row hits: " + tableArray.length);

//CHECK FOR MONTH (AUGUST)

//Using for
function month() {
//indices represents matches within var text and will be different for each row.
var indices = [];
//This executes for every row, 0 column.
for (var i = 0; i < tableArray.length; i++) {
//text represents the text contents of current position.
var text = tableArray[i][0];
//h represents each letter of text.
for(var h = 0; h < text.length; h++) {
if (text[h] === "A") {
// If we find it, add characters up to
// the length of month to the array
for(var j = h; j < (month.length + h); j++) {
indices.push(text[j]);
};
};
};

//Log every month (works)
//console.log(tableArray[i][0]);
};
return indices;
};

month("August");

//Alternative method
var indices = [];
var element = "August";
var idx = tableArray.indexOf(element);
while (idx != -1) {
indices.push(idx);
idx = tableArray.indexOf(element, idx + 1);
}
console.log(indices);

最佳答案

您没有在任何地方使用 month() 函数的参数。

function month(monthname) {
var indices = [];
$.each(tableArray(function(rowNum, row) {
$.each(row, function(colNum, col) {
if (col.indexOf(monthname) != -1) {
indices.push(col);
}
});
});
return indices;
}

关于javascript - 对数组进行字符串搜索不会返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32126795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com