Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Seeing All of Your Nested Data

Seeing All of Your Nested Data

Joan’s port rejects four rows of the October export. The website volunteer wants each rejected row kept whole, so whoever corrects the export can see it. The debugging result now holds the month, the total and the rejected list, and each entry holds its line, the row (date, donor and amount) and the reason. Each row sits inside an entry, inside the list, inside the result.

In Python, the end of main() builds that result as a dictionary and prints it:

Language: Python
    result = {
        "month": "October",
        "total": total,
        "rejected": rejected,
    }
    print(result)

Python prints the whole result on one long line, every level included, down to each row’s date, donor and amount. The reasons are Python’s own messages.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 Joan’s port rejects four rows of the October export. The website volunteer wants each rejected row kept whole, so whoever corrects the export can see it.

00:13 In this lesson, you’ll log that fuller result, find out why its rows seem to go missing, and see every level.

00:21 The result holds the month, the total and the rejected list. Each entry holds its line, the row with its date, donor and amount, and the reason. Each row sits inside an entry, inside the list, inside the result.

00:38 In Python, the end of main() builds that result as a dictionary and prints it. Run it, and Python prints the whole result on one long line. Every level is there, down to each row, like line 3’s date, donor and n/a amount.

00:57 The reasons are Python’s own messages.

01:01 In JavaScript, the port builds the same object. The keys lose their quotes. And total on its own is shorthand for total: total, a key named after the variable that holds its value.

01:15 The rejected list works the same way, and checkRows() already keeps each row inside its entry. Then console.log() does print()’s job.

01:27 Python printed every row. Your turn: pause the video and predict what JavaScript logs for each row.

01:40 Each row shows up as [Object]. The month, the total, every line and every reason are there.

01:50 Only the four rows look as if they were never stored. Read that way, you’d go back into checkRows() and change code that was right.

02:00 console.log() shows the top object and two levels of nesting below it. Here, the list of rejected entries, then each entry. Anything deeper it writes as [Object], or [Array] for an array, so big data stays readable.

02:18 The rows are still there.

02:22 Log only the rejected list, and the list becomes the top. Each row is now two levels down, so Node shows it in full.

02:32 To see every level at once, log the result with console.dir() instead. Unlike console.log(), it logs only one value, and takes an options object second.

02:44 Its depth option says how many levels below the top to show, and null, JavaScript’s value for nothing, means no limit. Run it, and each placeholder opens up into its row.

02:57 Without the depth option, console.dir() stops two levels down, as console.log() does.

03:06 Python’s plain pprint matches console.dir() with no depth limit: every level, over several lines. Node’s console.log() is like pprint with a depth of three, as pprint counts the top as one.

03:22 Where they part: pprint cuts only when you ask. For JSON, the way the result would be saved, JSON.stringify() with null and 2 does the job.

03:34 Its JSON text is indented two spaces per level, with every level there and every key in double quotes.

03:43 So in Node, [Object] means deeper, not missing. To see every level, log with console.dir() and a depth of null. When you need one part, log that part on its own.

03:59 Next up: Logging Values With Their Names, a text lesson. You’ll label each value you log with its name, as an f-string’s = sign does in Python, and meet console.table() and console.trace() for debugging.

Become a Member to join the conversation.