Like any kind of apps, there are difficult issues to solve when we write JavaScript apps.

In this article, we’ll look at some solutions to common JavaScript problems.

Difference between JSON.stringify and JSON.parse

JSON.strignify and JSON.parse do different things.

JSON.stringify converts an object into a JSON string.

JSON.parse parse a JSON string into an object.

For instance, we can use JSON.stringify by writing;

const obj = { foo: 1, bar: 2 };
const str = JSON.stringify(obj);

str is the stringified version of obj .

We can use JSON.parse by writing:

const obj = JSON.parse(str);

If str is a valid JSON string, then it’ll be parsed into an object.

Remove Accents or Diacritics in a String

If our string have accents or diacritics in a string, we can remove it by using the normalize method.

For instance, we can write:

const str = "garçon"
const newStr = str.normalize("NFD").replace(/[\u0300-\u036f]/g, "")

Then newStr is “garcon” .

NFD means the Unicode normal form.

It decomposes the combined graphemes into the combination of simpler ones.

Therefore the ç is decomposed into the letter and the diacritic.

Then we used replace to remove all the accent characters.

Parsing JSON Giving “unexpected token o” Error

We’ll get the ‘unexpected token o’ error if the thing that we’re trying to parse is already an object.

Therefore, we should make sure that we aren’t parsing a JSON object.

#web-development #programming #technology #javascript #software-development

JavaScript Tips — Base64 to Blob, JSON, Dates, and Input
2.75 GEEK