learn
Convert JSON to Array in Javascript
· ☕ 1 min read
Often my server DB functions return the data in JSON format with all fields and values thrown in. 1 const alphaNum = { a: 1, b: 2, c: 3, d: 4, e: 5 }; When we use charts (or even tables), this needs to be converted to an array of arrays.

Quickly Initiate Objects in Javascript
· ☕ 1 min read
Initializing objects can be a pain. You may end up with multiple checks if you are trying to set a specific prop in the object path to a pre-defined value. For e.g. set color of a Kawasaki bike to ‘blue’. To do this we have to traverse through the object path from the root through color.

Use Filter to Eliminate Duplicates in Javascript Array
· ☕ 1 min read
Use array filters to remove duplicates in an array. We have seen how we could use sets or an intermediate object in a loop to eliminate duplicates within array.There always is a better way of doing things. Why not use filter to make the code more readable and not use set at the same time :) ?

Search using Array.find() in Javascript
· ☕ 1 min read
Are you doing array search using the old school for-loops? Time to change, kid. Consider this array - 1 2 3 4 5 const fruits = [ { name: "apple", color: "red" }, { name: "orange", color: "orange" }, { name: "pear", color: "green" } ]; To search for a specific value -

GroupBy in Array using Reduce in Javascript
· ☕ 2 min read
There is a really cool way to do group by in an array using reduce. The problem statement - you have an array of objects and need to create different arrays based on similar attributes. This is very similar to the SQL group by statement. Consider the example array -

Manage Client Side Secrets in Javascript
· ☕ 2 min read
How do you maintain secrets on the client-side? In one sentence - there are no secrets client-side. If you don’t want anyone to know about the secret, you should not send it to the client. The need for secrets Client may need user ids/passwords, API keys, API secrets etc. None of them can be trusted to the client.

Convert Strings to Numbers in Javascript
· ☕ 2 min read
There are multiple ways to convert a given string to a number - so, which one to use and where? You would have also seen the more generic post on how to cast value from one type to another. But it may not be so apparent on which option to pick when you need to get numbers from a given string.

Default Function Arguments in Javascript
· ☕ 1 min read
You can easily introduce default values for the arguments passed to a function. Consider this function - 1 2 3 function sayHello(name) { return `hello ${name}`; } Calling this function without arguments will provide a not-so-pretty string. 1 console.log(sayHello()); // hello undefined Even if someone had an existential crisis, I doubt they will like to be called ‘undefined’.

Convert String to Title Case in Javascript
· ☕ 1 min read
We have a string.toLowerCase() and string.toUpperCase() in Javascript, but no out of the box function to convert to title case. Fear not if you are in that pickle - it is quite easy. 1 2 3 4 5 6 7 8 9 function ConvertTitleCase(str) { const strArr = str.toLowerCase().split(" "); for (let i = 0; i < strArr.

For Loop Performance in Javascript
· ☕ 3 min read
There are many types of for loops for an array - which should you use? How will you balance performance vs. code readability? Is that even a valid question? Let’s see a few cases. We will be seeing these distinct for loops today - for for-in forEach Note that the performance can be safely ignored for one off loops that do not hold up the user.

Spin-up a quick web server in Javascript
· ☕ 2 min read
Use these two quick ways to get a webserver up and running from any folder. 1. Use http-server Install http-server. 1 npm install http-server -g Navigate to any folder where you need a quick web server and start server. 1 2 cd client/dist http-server You can even proxy requests to a back-end server where required.

Date Object in Javascript
· ☕ 2 min read
Let’s see a few points about when to use a date object, and why use this object vs. a date library? The simplest and quickest way to get date in Javascript - 1 console.log(Date.now()); // 1549025852124 Date.now() does not create an object, and is really fast. But, it is also useless if you want to do anything with the date.

Arguments Object in Javascript
· ☕ 2 min read
Arguments object enables you to treat function arguments with respect.. and the developers with compassion. How do you pass arguments to a function? In its most primitive form: 1 2 3 function getSum(a, b) { return a + b; } This is perfectly ok in an ideal world where Buddha walks the earth.