Check if objects are empty in Javascript
· ☕ 2 min read
Check if a given object is empty without considering any custom props defined by prototype. Consider this sample object - 1 const earth = {}; Use Object.entries() or equivalent Object.entries gives us a short way to access entries in an array form. We could just check whether entries are empty to decide whether the parent object is empty.

Method of a Function in Javascript
· ☕ 2 min read
A function can have its own methods - strange but true. Why? Because everything in Javascript is an object and object can have methods. Consider - 1 2 3 4 5 6 function getSum(x, y) { return x + y; } console.log(getSum(1, 2)); //3 console.log(getSum.call(this, 1, 2)); //3 The first call to getSum is as expected.

Create Chainable Interfaces in Javascript
· ☕ 1 min read
Chainable interfaces make your code readable and, arguably, your life more enjoyable. Chainable interfaces are those series of methods that you see chained to an object. You can use them for objects where “relevant”. Commonly used functions It is common to see methods chained to one another. The chaining works from left to right, with results from the left-side of the function passed to the right in sequence.

Deleting Object Properties The Right Way in Javascript
· ☕ 1 min read
Do not set properties to null or empty, always use delete. Consider - 1 2 3 const earth = { name: "earth", position: 3, aweIndex: 42 }; console.log(earth); // { name: 'earth', position: 3, aweIndex: 42 } If you set any property to null - 1 2 3 earth["aweIndex"] = undefined; console.

Object vs. Prototype Properties in Javascript
· ☕ 2 min read
Check if a given object has a specified property. You can either use non-enumerable props or hasOwnProperty. Consider the below code - 1 2 3 4 Object.prototype.awesomeness = 42; const earth = {}; console.log(earth["awesomeness"]); // 42 We have defined a prototype that gets attached to any object. Therefore the prop defined by prototype is attached to the object as well.

Use MySQL Load Files Data in AdonisJS
· ☕ 4 min read
Use MySQL load data function to load data from files within within AdonisJS services. You can perform batch data and file operations efficiently using database utilities. The logic will likely be quicker, lighter on resources, and overall, more suited for batch jobs. Loading data from files is not as sought-after as in the good-ol’ days, but is quite common in enterprise applications.

Batch Operations in AdonisJS
· ☕ 2 min read
Use Adonis Scheduler to create batch tasks as well as schedule execution of said tasks. Adonis Scheduler improves your efficiency in writing batch jobs. I am talking about bulk operations that may or may not be suitable for your general purpose service and controllers. Although scheduler’s purpose seems to be, well, scheduling stuff we can reuse Tasks enabled by scheduler to run bulk operations.

Business layer vs. direct database operations
· ☕ 3 min read
I have this annoying behaviour to force as many things as I can through the business layer. This can be a boon and bane at the same time. What do I mean by business layer? Let’s say I have an application running on AdonisJS. For every operation (CRUD) - I try to go through controllers/services Almost all of them through APIs when called from front-end Almost all of the operations through Lucid with exceptions through Database statements Use Tasks or equivalent for batches Everyone does this, why is it a point of discussion?

Quickly Check if Array Elements Satisfy Given Criteria in Javascript
· ☕ 1 min read
Use Array.some() and Array.every() to check whether an array has elements that satisfy the given criteria. Consider the below array - 1 const nums = [3, 5, 6, 1, 2]; You can check whether the elements satisfy a specified criteria with below code - 1 2 console.

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 -