1

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:

enter image description here

Is there some method to get this output in plain text, or should I write a custom parsing function?

5
  • JSON.stringify(array_multi) for text conversion Commented Mar 23, 2019 at 11:49
  • 3
    array_multi should really be an object, not an array! Commented Mar 23, 2019 at 11:51
  • If you're doing this manually, the Firefox console lets you right-click on an Object that's been logged to the console, and you can select "Copy object". That will do the JSON conversion for you, so only JSON-serializable properties are included. Commented Mar 23, 2019 at 12:00
  • Is this tagged NodeJS because it is running on NodeJS? Commented Mar 23, 2019 at 12:29
  • @Jonas Wilms yes I run inside a node js script and need variable with text for send w/ node mail extesion Commented Mar 23, 2019 at 12:33

3 Answers 3

5

If you are using JSON.stringify, you'll get the complete data, however there are a few downsides:

  • Arrays string properties, functions and other data structures get ignored completely (therefore serializing your data as is won't work¹)
  • circular references fail to serialize
  • There is no way to see inheritance from that

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why the downvote? I precisely answers the OP's question.
I am using the OP's data while precising that it should be an object instead of an array
Yes yes sorry, I saw that. (I didn't downvote.) I think this is probably the best the OP can do, but it should be noted that not all object graphs can be serialized as JSON. (Well a custom serializer would be the next step I guess.)
0

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));

Comments

-1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.