charlie_cat
charlie_cat

Reputation: 1850

Debugging javascript and console.log not working

I not sure how to do this, but I am trying to debug some JS values with console.log:

 function SetAPIValue(key1, value1, scormVersion, methodCalled) {
/* key1 value sometimes contains the value true or false and dont want to store these! */
    if (key1 == true || key1 == false) { }
    else
    {
        /* value1 comes back with two values for cmi.suspend_data, then 3, then 4 all in one string seperated by a ';'! */
      setValuesArray.push({
       key: key1,
       value: value1
     });

   for (var i = setValuesArray.length - 1; i >= 0; i--) {
   var array = setValuesArray[i];
   console.log(setValuesArray);
   if (array.key == key1) {
     if (key1 = "cmi.suspend_data") {
       if (value1.indexOf(";") > 0)
       {
         valueArray[j] = value1.split(";");
         alert(valueArray[j]);
         //alert("key1 is" + key1 + "and is returning " + setValuesArray[j-1].value);
         return valueArray[j];
        }
       }
       else
         return setValuesArray[i].value;
       } 
    }
  }
  return "";
}

i am trying to see what is in console.log(setValuesArray). Whne i run my app in IE9 I have F12 developer tools enabled, I click on the console tab, and all I get there is:

LOG: [object Object] LOG: [object Object],[object Object] LOG: [object Object],[object Object] LOG: [object Object],[object Object],[object Object]

I dont understand? how can I see the values in setValuesArray? I do not have firebug-lite installed. I work in win 7 64bit Thanks

Upvotes: 2

Views: 3911

Answers (2)

Borislav T
Borislav T

Reputation: 618

I think that using console.log is the better way to go (when compared to using alerts).

However, keep in mind that it both console.log() and alert() will show the string representation of the input param and for JS objects that's "[object Object]".

What I can recommend is to stick to the idea of using console.log(), but you might want to take a look at this article if you wish to be able to have this working in older browsers (prior versions of IE in most cases): http://patik.com/blog/complete-cross-browser-console-log/

Also, when you wish to print a string depicting the contents of a JS object, you can use JSON.stringify(). Since, of the Internet Explorers, only 7 is the one that doesn't have native support for the JSON objecт. But you can easily get around that by including the json2.js library to your page - the code is available in the GitHub repository here.

Upvotes: 1

ymutlu
ymutlu

Reputation: 6715

You are trying to log object thats why you are getting these outputs. Try the log below or use toString method for your object.

   for (var i = setValuesArray.length - 1; i >= 0; i--) {
     var array = setValuesArray[i];
     console.log("key: "+setValuesArray[i].key+ " value: "+setValuesArray[i].value);
     ...
   }

Upvotes: 1

Related Questions