pretty-print JSON using JavaScript

Pretty-printing is implemented natively in JSON.stringify(). The third argument enables pretty printing and sets the spacing to use: If you need syntax highlighting, you might use some regex magic like so: See in action here: jsfiddle Or a full snippet provided below: Show code snippet

Javascript Uncaught TypeError: Cannot read property ‘0’ of undefined

The error is here: You are passing the first item of words, instead of the array. Instead, pass the array to the function: Problem solved! Here’s a breakdown of what the problem was: I’m guessing in your browser (chrome throws a different error), words[] == words[0], so when you call hasLetter(“a”,words[]);, you are actually calling hasLetter(“a”,words[0]);. So, in essence, … Read more

Loop inside React JSX

Think of it like you’re just calling JavaScript functions. You can’t use a for loop where the arguments to a function call would go: See how the function tbody is being passed a for loop as an argument – leading to a syntax error. But you can make an array, and then pass that in as an argument: You can basically … Read more

Syntax for an async arrow function

Async arrow functions look like this: Async arrow functions look like this for a single argument passed to it: Async arrow functions look like this for multiple arguments passed to it: The anonymous form works as well: An async function declaration looks like this: Using async function in a callback: Using async method inside of a class:

SyntaxError: Unexpected token o in JSON at position 1

The JSON you posted looks fine, however in your code, it is most likely not a JSON string anymore, but already a JavaScript object. This means, no more parsing is necessary. You can test this yourself, e.g. in Chrome’s console: JSON.parse() converts the input into a string. The toString() method of JavaScript objects by default returns [object Object], resulting … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)