0

I have a public database of information that I want to reformat using jQuery, JavaScript (and later HTML and 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);
1
  • Why are you passing an argument to month()? It doesn't take any parameters. Commented Aug 20, 2015 at 19:43

2 Answers 2

1

You're not using the argument to the month() function anywhere.

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Using jQuery you could as well just do this:

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>

1 Comment

Thank you, trying that out briefly it seems to work very well. I've never used jQuery, but it seems to be the best way to get this job done. I'll try taking that and creating a new element id from the results soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.