0

I'm a little green as you can tell by my post history here, but i am trying to get the key, value for a JSON object and its not outputting as i thought it would. What am I doing wrong?

(function($){
    jQuery(document).ready(function(){
        var tableRows = [];
        var headersText = [];
        var $headers = $("th");
        var $rows = $("#table tbody tr").each(function(index) {
          $cells = $(this).find("td");
          tableRows[index] = {};
          $cells.each(function(cellIndex) {
            if(headersText[cellIndex] === undefined) {
              headersText[cellIndex] = $($headers[cellIndex]).text();
            }
            tableRows[index][headersText[cellIndex]] = $(this).text();
          });    
        });
        var tableData = {
            "tableData": tableRows
        };
        /*alert(JSON.stringify(tableData));*/

        $.each([tableData], function(key, value){
            console.log( key + ":" + value );
        });
    });
})(jQuery);

in console I'm getting:

0:[object Object]

instead of (example):

0:[NAME SAMPLE-NAME]

2 Answers 2

5

That's what happens when an object is turned into a string, and concantenating strings and objects, the object will be converted to a string, and the string representation of an object is [object Object]

console.log( key + ":" + value ); // you're concantenating strings and objects

Try this instead

console.log( key, value );

As a sitenote, $.each iterates both objects and arrays, so there's no need to wrap your object in an array, or your array in an object ?

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

3 Comments

Or console.log({key:key,value:value})
This worked, now how would i get for example the value of key: NAME in this object?
You'd have to know where that key was, and do something like tableData.tableData.path.to.NAME, or just iterate until you find it ?
1

The problem isn't really the console.log, it's your iteration.

You mean to do this I think:

 $.each(tableData["tableData"], function(key, value){
      console.log( key, value );
 });

Not

 $.each([tableData], function(key, value){
      console.log( key, value );
 });

In your original code, you were creating an array of one element - [tableData] and then iterating over that, which doesn't really make any sense to do. TableData is a hash - a hash doesn't have a key and a value, it has a set of key and values. You need to iterate over the hash instead.

4 Comments

This worked, now how would i get for example the value of key: NAME in this object?
I think you're slightly confused over your data structure. You have var tableData = {"tableData": tableRows}; but tableRows itself is an array. Is it possible that it's tableData["tableData"] that you want to iterate over?
yea thats it exactly. thanks for helping me learn :) so now i could console.log( key["NAME"], value ) to target that specifically?
Glad to help :) Let me know if there's any other questions you have.

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.