8

I was wondering if there was a quick way to get all of an object's variable's values, similar to php's var_dump() method.

So if I had an object

var myObject = {
    Up: 38,
    Dn: 40,
    Lf: 37,
    Rt: 39,
    Enter: 13,
    Space: 32,
    Esc: 27
};

The string I would get back would look something like

[ Up:38, Dn:40, Lf:37, Rt:39, Enter:13, Space:32, Esc:27 ]

Let's say I need to do this on a computer where I can't use firebug. Is there any way to do this without iterating through all the parameters in an object? Is there a standalone library that has something like this?

Hans Z
  • 4,664
  • 2
  • 27
  • 50
  • @FelixKling not a JSON object, would it still work? – Hans Z Jun 13 '12 at 21:08
  • 2
    @Hans. There is no such thing a JSON object. – gdoron Jun 13 '12 at 21:08
  • 1
    `JSON.stringify` converts arrays and objects to JSON. Nothing more, nothing less. Regarding gdoron's comment, have a look at http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/ – Felix Kling Jun 13 '12 at 21:09
  • @FelixKling Oh wow, I guess I haven't really worked with JSON before... that's really interesting – Hans Z Jun 13 '12 at 21:12
  • possible duplicate of [Is there an equivalent for var_dump (PHP) in Javascript?](http://stackoverflow.com/questions/323517/is-there-an-equivalent-for-var-dump-php-in-javascript) -- provides a list of possibilities (also my suggestion). – Felix Kling Jun 13 '12 at 21:13
  • Hmm JSON.stringify seems to work on objects that aren't handles/don't have handles to jquery selected elements. – Hans Z Jun 13 '12 at 21:15
  • @FelixKling I tried the methods mentioned in that post, but it doesn't work for me. I think I'll try forking to a different question. – Hans Z Jun 13 '12 at 21:17
  • Mmmh. The accepted answer seems to be ok. But yeah, if you have complex objects, it's coing to be more difficult. Then you should really use Firebug Lite. – Felix Kling Jun 13 '12 at 21:18

5 Answers5

8

As a quick one liner I often use

   var o = {a:1, b:2}, k, s = []; for (k in o) o.hasOwnProperty(k) && s.push (o[k]); s = s.join (', ');

You only need to change one occurence of the object (value of o) and the result is in s.

This does not recurse into the data structure. JSON.stringify is probably more suited if that is a requirement. Note however that JSON.stringify does not do functions, it simply skips them!

For formatted stringify use

JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces

As per answer to Javascript: How to generate formatted easy-to-read JSON straight from an object?

Community
  • 1
  • 1
HBP
  • 15,685
  • 6
  • 28
  • 34
5

Using the default dev. tools on IE, Chrome and Firefox

console.dir(myObject); 

If you really can't use these tools then maybe a JSON.stringify(myObject) could help.

Ryan
  • 1,797
  • 12
  • 19
  • 1
    "Let's say I need to do this on a computer where I can't use firebug." – j08691 Jun 13 '12 at 21:08
  • No dev tools can be used at all? Look into JSON.stringify. – Ryan Jun 13 '12 at 21:10
  • Right, on IE console.dir dumps all the info of the object but console.log doesn't. On Chrome, console.log works fine. I'll edit the answer to include that. – Ryan Jun 13 '12 at 21:14
2

Have you tried FirebugLite ? It has a lot of Firebug functions.

This is Javascript Firebug library, you need only to load script

<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>

And you will get Firebug console in all major browsers including Internet Explorer

Slawek
  • 583
  • 3
  • 9
1

JSON.stringify is the way to go, but it works on all browsers, just include the lib:

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

Example:

        text = JSON.stringify(['e', {pluribus: 'unum'}]);
        // text is '["e",{"pluribus":"unum"}]'


        text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
        // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'

        text = JSON.stringify([new Date()], function (key, value) {
            return this[key] instanceof Date ?
                'Date(' + this[key] + ')' : value;
        });
        // text is '["Date(---current time---)"]'
mkoryak
  • 57,086
  • 61
  • 201
  • 257
0
function var_dump(obj) {
    var obj_members = "";
    var sep = "";
    for (var key in obj) {
        obj_members += sep + key + ":" + obj[key];
        sep = ", ";
    }
    return ("[" + obj_members + "]");
}
kevin628
  • 3,458
  • 3
  • 30
  • 44