0

I have a very simple data that i want to display in table.

DATA

[{"1":"Red","2":"Orange","3":"Blue","4":"Yellow"}]

CODE:

 var a = [];
                    $.each(data, function (key, val) {
                        a.push('<tr id="tr_' +key+ '">');
                        a.push('<td>' + key+ '</td>');
                        a.push('<td>' + val+ '</td>');
                        a.push('</tr>');
                    });
                    var s = a.join('');
                    $('#color-list').html(s);

The above code resulted in [object Object] instead of the Data.

Below is my expected result

1 Red

2 Orange

3 Blue

4 Yellow

Any suggestion will be greatly appreciated

1
  • You may very well use a dedicated jQuery plugin to populate data from JSON to HTML github.com/anuary/jquery-json-to-table. You are playing with raw HTML, rather than DOM. Which isn't really practical or safe. Since you already using jQuery you might just learn to use .text() and .html(). Commented Aug 19, 2012 at 12:26

4 Answers 4

1

Your data variable is an array (containing only one element). The object is actually the first element of that array. So use:

$.each(data[0], function (key, val) {
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! That exactly what I want.
1

Your data is an array of 1 value which is an object. If you loop through the array you'll get one row in your table with that object as the value.

Instead you should remove the [] and have just the object and then go through the keys/values of that instead:

var data = {"1":"Red","2":"Orange","3":"Blue","4":"Yellow"}

This then gives the result you want.

If you can't get rid of the [ ] for some reason (e.g. external data source) then just use data[0] in your loop instead of data to get the object from the first position in the array.

Comments

0

You can just loop around in this way:

 // If the returned data is an object do nothing, else try to parse
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
for (var i = 0; i < array.length; i++) {
    str += '<tr>';
    for (var index in array[i]) {
        str += '<td>' + array[i] + '</td><td>' + array[i][index] + '</td>';
    }
    str += '</tr>';
}

Comments

0

Actually Sometimes it doesnt works when json is in string form. Please try the following:

[{"Srno":38,"ReqId":36,"ItemId":155,"Qty":90.00,"TransferItemId":null,"TransferQty":null,"ExpReqDated":"\/Date(1363285800000)\/","IsCancelled":false,"Remarks":null},d":171,"Qty":40.00,"TransferItemId":null,"TransferQty":null,"ExpReqDated":"\/Date(1363717800000)\/","IsCancelled":false,"Remarks":null},{"Srno":113,"ReqId":53,"ItemId":172,"Qty":40.00,"TransferItemId":null,"TransferQty":null,"ExpReqDated":"\/Date(1363717800000)\/","IsCancelled":false,"Remarks":null}]"

Then We have cast json by JSON.parse(data) to convert it in array...

Comments

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.