I need to send an email that contains the console.log
output of a JS object. Here a code example:
let array_multi = [];
array_multi["07:00"] = ["one","two","three"];
array_multi["08:00"] = ["foo","bar","foo"];
array_multi["09:00"] = ["lorem"];
console.log(array_multi);
In my console result like this:
Is there some method to get this output in plain text, or should I write a custom parsing function?
If you are using JSON.stringify , you'll get the complete data, however there are a few downsides:
In your case you could do:
let array_multi = {}; array_multi["07:00"] = ["one","two","three"]; array_multi["08:00"] = ["foo","bar","foo"]; array_multi["09:00"] = ["lorem"]; // logs as object console.log(array_multi); console.log(typeof array_multi); // parse and convert to string console.log(JSON.stringify(array_multi)); console.log(typeof JSON.stringify(array_multi));
In Node.js you've got another option, which is util.format , which will return the same content as a string that you can see in the Node.js console. While it does give you a great insight into the different datatypes and relationships, it cannot show you the same infinite tree that an interactive console is able to show, so it will only show you a small part of the big picture.
¹: Your array_multi
should actually be an object, not an array, as arrays should only have numeric keys.
After a lot of search the right method is write a custom function (chrome have once inside dev tools core)
here the solution:
let array_multi = []; array_multi["07:00"] = ["one","two","three"]; array_multi["08:00"] = ["foo","bar","foo"]; array_multi["09:00"] = ["lorem"]; function print_js_element(elm, show_content = false){ let output_res = "{\\n"; let object_keys = Object.keys(elm); object_keys.some(function(key,index) { output_res += "\\t" + key + ": (" + elm[key].length + ")"; if(show_content){ output_res += " " + JSON.stringify(elm[key]); } output_res += "\\n"; }); output_res += "\\n}"; return output_res; } console.log(print_js_element(array_multi,false)); console.log(print_js_element(array_multi,true));
Covert this array into an object first:
let array_multi = []; array_multi["07:00"] = ["one","two","three"]; array_multi["08:00"] = ["foo","bar","foo"]; array_multi["09:00"] = ["lorem"]; let arrObj = {...array_multi}; console.log(JSON.stringify(arrObj));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.