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?
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?
Share Improve this question edited Jun 14, 2012 at 21:38 Hans Z asked Jun 13, 2012 at 21:05 Hans ZHans Z 4,7442 gold badges28 silver badges51 bronze badges 8 | Show 3 more comments5 Answers
Reset to default 9As 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?
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.
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
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---)"]'
function var_dump(obj) {
var obj_members = "";
var sep = "";
for (var key in obj) {
obj_members += sep + key + ":" + obj[key];
sep = ", ";
}
return ("[" + obj_members + "]");
}
JSON.stringify
converts arrays and objects to JSON. Nothing more, nothing less. Regarding gdoron's comment, have a look at benalman.com/news/2010/03/theres-no-such-thing-as-a-json – Felix Kling Commented Jun 13, 2012 at 21:09