简体   繁体   English

在数组上进行字符串搜索不返回任何内容

[英]String search on an array returns nothing

I have a public database of information that I want to reformat using jQuery, JavaScript (and later HTML and CSS). 我有一个公共信息数据库,我想使用jQuery,JavaScript(以及后来的HTML和CSS)重新格式化。 I'm trying to create new groups within the table using information provided. 我正在尝试使用提供的信息在表中创建新组。 In this specific example, I want to find every instance the month of August appears in a table. 在此特定示例中,我想查找表中出现的每个实例。

I've tried two methods to search the content, and both return an empty array ( '[]' ). 我尝试了两种方法来搜索内容,并且都返回一个空数组('[]')。 This is confusing because I can log the content in the console, but it won't add to a new array. 这很令人困惑,因为我可以在控制台中记录内容,但是不会将其添加到新数组中。

//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);

You're not using the argument to the month() function anywhere. 您没有在任何地方使用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;
}

Using jQuery you could as well just do this: 使用jQuery,您还可以这样做:

 function month (elem, needle) { elem.find("td:contains('"+needle+"')").each(function() { console.log(this); }) } month($('table'), "AUGUST"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table> <tr> <td>AUGUST</td> </tr> <tr> <td>MAY</td> </tr> <tr> <td>APRIL</td> </tr> <tr> <td>JUNE</td> </tr> <tr> <td>AUGUST</td> </tr> <tr> <td>SEPTEMBER</td> </tr> <tr> <td>AUGUST</td> </tr> <tr> <td>JUNE</td> </tr> <tr> <td>AUGUST</td> </tr> <tr> <td>MAY</td> </tr> </table> 

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

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