JQuery quickly flushes objects

I was wondering if there is a quick way to get all the values โ€‹โ€‹of the object variables similar to the php 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 line that I will return to will 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 cannot use firebug. Is there a way to do this without repeating all the parameters in the object? Is there a separate library that has something like this?

+4
source share
5 answers

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 appearance of the object (value o), and the result - in s.

This does not apply to the data structure. JSON.stringify is probably more suitable if necessary. Note, however, that JSON.stringify does not perform functions, it just skips them!

To format stringify use

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

As an answer to Javascript: how to generate formatted easy-to-read JSON directly from an object?

+5
source

Using default by default. Tools for IE, Chrome and Firefox

 console.dir(myObject); 

If you really can't use these tools, JSON.stringify(myObject) may help.

+4
source

Have you tried FirebugLite ? It has many Firebug features.

This is the Javascript Firebug library, you only need to download the script

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

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

+2
source

JSON.stringify is the way to go, but it works in all browsers, just enable 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---)"]' 
+1
source
 function var_dump(obj) { var obj_members = ""; var sep = ""; for (var key in obj) { obj_members += sep + key + ":" + obj[key]; sep = ", "; } return ("[" + obj_members + "]"); } 
0
source

All Articles