/    Sign up×
Community /Pin to ProfileBookmark

Display Object contents in console.log

I can’t get the contents of an object to display in the console.log correctly on different browsers.
Here is the script in question:

[code]
function test1b() { // new way
var arr = [
{“name”:”apple”, “count”: 2},
{“name”:”orange”, “count”: 5},
{“name”:”pear”, “count”: 3},
{“name”:”orange”, “count”: 16},
];

var newArr = arr.filter(function(item){
return item.name === “orange”;
} );
// console.log(“Filter results:”,newArr);
console.log(“Filter results:”,newArr.toSource()); // OK for FF
// console.log(“Filter results:”,newArr.toString());
}
test1b();
[/code]

If you run it in Firefox it displays correctly with the .toSource() method
but it displays <unavailable> or [object Object],[object Object] for the Chrome browser.
If you use .toString() instead, it does not display the object contents correctly in either browser
Similar results if you leave off the .toString() or .toSource altogether.
The different statements are (un-)commented in the last 3 lines of the ‘test1b’ function

Why can I display the object contents correctly in the console.log
ONLY in FF with the .toSource() method? Is there some special setting
for the console.log output that need to be set to see the contents correctly?
Why does the console.log display either <unavailable> or [object Object] and not the contents?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumDec 08.2019 — MDN says that toSource() is implemented in FF only:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toSource

I'm using Opera and when logging an array or an object I'm using your first method without toString() or toSource. It produces a clickable output I was always happy with.
Copy linkTweet thisAlerts:
@JMRKERauthorDec 09.2019 — Thank you 'Sempervivum'. I had read that reference and believed them.

What I was looking for is a way that displays the object contents without a special browser.

Just seems unreasonable for a user to need to use a specific browser for a function to work correctly.

I guess I could check for the type of browser and adjust the output for what is found, but that also

seems to be just a wasteful hack especially if changes occur to the browser that make it defunct in the future.
Copy linkTweet thisAlerts:
@SempervivumDec 09.2019 — IMO the console is designated for use by a developer, not for a common user. And most developers have a preferred browser and they are familiar with the console and the developer tools in general.

When it is required to output an object in a readable format one might consider to convert it to JSON and output it in the console or a HTML container. AFAIK JSON is available in all current browsers.
Copy linkTweet thisAlerts:
@JMRKERauthorDec 10.2019 — I did not think of the console as a specific browser developer tool, but you may know more about that than I do.

I think of the console as an alternative to the use of alert() messages scattered throughout a script.


And while I am not a developer, I do dabble with programming and wish to have all my scripts work in all browsers without the need to debug with specialized tools that work differently in the various browsers.

I'll look into the creation of a helper script using JSON however my initial impression is that it would be cumbersome to use as a development tool.

Thank you for your thoughts.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...