0

I'm fine with using javascript or jquery

I added in another column from the database table to get returned and what I currently see is

122461,4876192

As now I have 2 records

So with that is shows

enter image description here

 $.each(alldata.Data, function (index, query) {
    strData += "<td>" + query + "</td>"; 
 }

That is spitting out 122461,4876192 how do I use "query" for separation?

I want to end up doing this

<td>122461</td><td>4876192</td>

3 Answers 3

1

EDIT: the picture you showed is showing an array of arrays. The reason you are getting comma separated values is because the variable query is actually an array, so when you join it with a string, JS is just listing the values of the subarray in CSV format.

Personally, I think you don't even have to use jQuery for this task, sometimes it's a lot easier to use a couple of nested for loops. You have to loop through the main array, and then each of the subarrays and join them inside of <td> tasks. After that, you can use jQuery for anything you want to do with the elements.

var strData = "";
if (allData.Data) {
    for (var i = 0; i < allData.Data.length; i++) {
         if (allData.Data[i]) {
             for (var j = 0; j < allData.Data[i].length; j++) {
                 strData += '<td>' + allData.Data[i][j] + '</td>';
             }
         }
    }
}

However, if you really want to use the jQuery each function, you can do

var strData = "";
if (allData.Data) {
    $.each(allData.Data, function(i, query) {
        // Note this will skip everything that equals to false in JavaScript
        // not just null, for example, "", false, 0, etc...
        if (!query) return;
        $.each(query, function(j, value) {
            // See note above
            if (!value) return;
            strData += '<td>' + value + '</td>';
        });
    });
}

There's a variety of shortcuts you can take using built-in array functions, such as join as @zan suggested in his answer.

EDIT 2: Added null checks EDIT 3: Added null checks to jQuery

Sign up to request clarification or add additional context in comments.

5 Comments

The native Array.prototype.forEach function would also be a good choice.
for (var j = 0; j < alldata.Data[i].length; j++) { that line is throwing an error Uncaught TypeError: Cannot read property 'length' of null
That means one of the subarrays of the allData object is null. Just add an condition to check if it's not null, you could do: var strData = ""; for (var i = 0; i < allData.Data.length; i++) { if (allData.Data[i]) { for (var j = 0; j < allData.Data[i].length; j++) { strData += '<td>' + allData.Data[i][j] + '</td>'; } } }
I am using the non-jquery code as the jquery code seems to not work as it was not handling the nulls
@BrianThornton I added null checks to jQuery, note the checks will skip all objects that equate to false, not just null. This shouldn't be a problem in your case though. If it's still causing a problem, would you be able to post some sample data that's causing the loops to fail. It works on the data I've tested it with.
0

What you need is

var strData = ''
$.each(alldata.Data, function (index, query) {
    strData += "<td>" + query.join('</td><td>') + "</td>"; 
 })
 console.log(strData)

Why That is spitting out 122461,4876192 how do I use "query" for separation?

Because you are concatenating Array with String

"myString" + [1,2,3,4] + "otherString"

will give you this output

"myString1,2,3,4otherString"

Comments

0

Try this:

If query is string:

$.each(alldata.Data, function (index, query) {
  strData +='<td>'+query.split(',').join('</td><td>')+'</td>';
}

if Query is array then use this:

$.each(alldata.Data, function (index, query) {
  strData +='<td>'+query.join('</td><td>')+'</td>';
}

This will also works when you have more the 2 elements in array

2 Comments

am I doing something wrong ? doing a split seems a bit crazy
Everything seems fine to me, getting it as array element will even provide us with many different functionality too , like sorting numbers etc..

Your Answer

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