Skip to main content
View on YouTube to comment and like!

Syntax Video

Console Log [Object]

Chapter 1 00:00

Transcript

has this ever happened to you you're trying to just see what kind of data is coming in maybe from an API or the database either way you have a nested data structure you say let me go ahead and check out that data and you Council log that data because that's what we do right and then you're hit with this object or in sometimes object object and you say hey I'm trying to see what this data is and I can't see anything because it says object here well how do you fix this now while I have my own technique I figured I would go to stack Overflow and then try several of the options that people suggested as a solution there the first one being to use Json stringify data now if you use Json stringify and then pass in your data here we can rerun this we can hit run and see that the console log this time does in fact give us all of the information so ding Json

stringify does work for getting all of the data out of a nested object now let's go ahead and try another suggested solution which was console dir which should hopefully give you access to this now I can confirm that this does work in browser but we're in a node context here so console dir let's see if this is a good replacement especially if you're working in isomorphic apps this time it gives you the same output as console log now console Dirk can give you some cool interactivity in the browser but in node context we're getting hit with the same exact output now I saved this last one for last because it is my preferred solution and in my opinion it is the nicest solution this is Json stringify data but instead of just straight up Json stringifying the data we're passing in some options to Json stringify in fact let me put this on some new lines so you can see

what those options are for the first parameter is data the second parameter in Json stringify is going to be null why well this is what's called the replacer it's not something we need for this and the last value is going to be the amount of spacing now what does spacing get you in this instance it gets you some beautiful formatting in the output so instead of getting a jumbled mess of who knows what and instead of getting an object instead of our data we're getting a beautifully output version of our data in an object structure very similarly to how we wrote it in the first place so if you're out there looking to not have object in your console logs just pop in a Json stringify with data null and then this is however many spaces you want if you want it tighter this can be one if you wanted a wider spaced out you can have this be anything you'd like some people use four now another quick tip is that you can always create a log function so you can

have a function log where you simply console log out the data here and then instead of running a console log like this you can just run your log function and say log data this should give us the same predictable way of doing our logging every single time then we don't have to run a whole Json stringified business and have all this kind of ugly extra code in here right we get to just wrap it up in a neat log not to mention if you're using some sort of error tracking like Sentry or something like that you could then keep track of your logs in some other type of service as well perhaps if this is going to be logging an error specifically so as always thanks for watching I'll see you in the next

Quote it with credit and a link. Quoting Syntax