JavaScript - Merge two arrays according to id property

AmitDiwan
Updated on 15-Mar-2026 23:19:00

5K+ Views

When working with JavaScript arrays of objects, you often need to merge two arrays based on a common property like an ID. This is useful for combining related data from different sources, such as user profiles and addresses. Suppose we have two arrays of objects. The first contains user IDs and names, while the second contains user IDs and addresses: const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ... Read More

Masking email to hide it in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

4K+ Views

It is a common practice that when websites display anyone's private email address they often mask it in order to maintain privacy. For example, if someone's email address is: const email = 'ramkumar@example.com'; It is displayed like this: r...r@example.com We need to write a JavaScript function that takes an email string and returns the masked email for that string. Basic Email Masking Function Here's a simple approach that masks the username part of the email: const email = 'ramkumar@example.com'; const maskEmail = (email = '') => ... Read More

Finding the missing number between two arrays of literals in JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

688 Views

Finding the missing number between two arrays is a common programming problem where one array is a shuffled version of another with exactly one element removed. Problem Statement We need to write a JavaScript function that takes two arrays: arr1 (original) and arr2 (shuffled duplicate with one missing element). The function should identify and return the missing element. Example const arr1 = [6, 1, 3, 6, 8, 2]; const arr2 = [3, 6, 6, 1, 2]; const findMissing = (arr1 = [], arr2 = []) => { const obj = ... Read More

Sorting 2-D array of strings and finding the diagonal element using JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

232 Views

Problem We are required to write a JavaScript function that takes in an array of n strings. Each string in the array consists of exactly n characters. Our function should first sort the array in alphabetical order, then return the string formed by the characters present at the principal diagonal starting from the top left corner. Understanding the Process The solution involves two main steps: Sort the array of strings alphabetically Extract diagonal characters where row index equals column index (i === j) Example ... Read More

process.env() Method in Node.js

Mayank Agarwal
Updated on 15-Mar-2026 23:19:00

568 Views

The process.env property in Node.js provides access to environment variables. It returns an object containing all environment variables available to the current process, making it essential for configuration management in Node.js applications. Syntax process.env Note: process.env is a property, not a method, so it doesn't require parentheses. Parameters process.env is a read-only object that doesn't accept parameters. It automatically contains all environment variables from the system where the Node.js process is running. Example: Accessing All Environment Variables Create a file named env.js and run it using: node env.js ... Read More

How to use JavaScript to replace a portion of string with another value?

Mayank Agarwal
Updated on 15-Mar-2026 23:19:00

984 Views

In this article, we are going to explore replacing a portion of a string with another value using JavaScript. We can replace string parts in multiple ways. Below are some common methods − replace() method split() and join() methods replaceAll() method Let's discuss the above methods in detail. Using replace() Method The replace() method is an inbuilt JavaScript method that replaces the first occurrence of a substring or regular expression pattern with a new value. The original string remains unchanged. Syntax ... Read More

How to disable the centered scaling of Rectangle using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

330 Views

In this tutorial, we are going to learn how to disable the centered scaling of Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect class and add it to the canvas. When being scaled via controls, assigning a true value to the centeredScaling property, uses the center as the object's origin of transformation. Syntax new fabric.Rect({ centeredScaling: Boolean }: Object) Parameters Options (optional) − ... Read More

How to set the baseline of superscript with Text using FabricJS?

Rahul Gurung
Updated on 15-Mar-2026 23:19:00

320 Views

In this tutorial, we are going to learn how to set the baseline of superscript with Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. We can also use the superscript property where we can specify its baseline. Syntax new fabric.Text(text: String , { superscript : {"size": Number, "baseline": Number}: ... Read More

Display selected checkboxes on another page using JavaScript?

Kalyan Mishra
Updated on 15-Mar-2026 23:19:00

4K+ Views

In this article, you will learn how to pass selected checkbox values from one page to another using JavaScript and localStorage. Checkboxes allow users to select multiple values, unlike radio buttons which only allow single selection. When a checkbox is checked, it represents a "true" value indicating user selection. Basic Checkbox Example Example checkbox This creates a simple checkbox that shows an alert when clicked. Getting Checkbox Values on Same Page First, let's see how to collect checked values within the same ... Read More

How to validate if an element in an array is repeated? - JavaScript

AmitDiwan
Updated on 15-Mar-2026 23:19:00

358 Views

We are required to write a JavaScript function that takes in two arguments: An Array, say arr, of literals that may contain some repeating elements. A number, say limit. The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise. Using Object to Count Occurrences The most efficient approach is to use an object to count each element's occurrences, then check if any count exceeds ... Read More

Advertisements