Articles on Trending Technologies

Technical articles with clear explanations and examples

Relative sorting in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 396 Views

Suppose, we have two arrays, let's say arr1 and arr2. The elements of arr2 are distinct, and all elements in arr2 are also in arr1. We are required to write a JavaScript function that takes in two such arrays and sorts the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order. For example− If the two input arrays are − const arr1 = [2, 3, 1, 3, 2, 4, 6, ...

Read More

Test for existence of nested JavaScript object key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Testing for the existence of nested JavaScript object keys is a common requirement when working with complex data structures. This prevents errors when accessing deeply nested properties that may not exist. The Problem Accessing nested properties directly can throw errors if intermediate keys don't exist: let test = {}; // This would throw an error: // console.log(test.level1.level2.level3); // TypeError: Cannot read property 'level2' of undefined Using a Custom Function We can create a function that safely checks each level of nesting: const checkNested = function(obj = {}){ ...

Read More

Formatting Software License Key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 285 Views

Problem We need to write a JavaScript function that formats a software license key by reorganizing alphanumeric characters into groups of a specified length, separated by dashes. The function takes a string containing alphanumeric characters and dashes, removes existing dashes, converts all letters to uppercase, and regroups the characters. The requirements are: Remove all existing dashes from the input string Convert all lowercase letters to uppercase Group characters into sections of length K (except the first group, which can be shorter but must contain at least one character) Separate groups with dashes Example Input and ...

Read More

How to swap two array elements in JavaScript?

Kalyan Mishra
Kalyan Mishra
Updated on 15-Mar-2026 5K+ Views

In this tutorial, we explore different approaches to swap two array elements in JavaScript. For example, we can swap the first and second elements of an array: Input: ["first", "second", "third", "fourth", "fifth"] Output: ["second", "first", "third", "fourth", "fifth"] Here we swapped "first" and "second" values of the array. We will explore three different methods to swap array elements: Using a temporary variable Using Array Destructuring (ES6) Using Array splice() Method Using a Temporary Variable ...

Read More

Adding fade-in and fade-out animation to a Polygon object using FabricJS

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 588 Views

We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to add fade-in and fade-out animation, we can use the opacity property in conjunction with animate method. Syntax animate(property: String | Object, value: Number | Object): fabric.Object | fabric.AnimationContext | Array. Parameters property ...

Read More

What is the best way to reduce and merge a collection of objects – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 709 Views

The best way to reduce and merge a collection of objects is to use Object.values() combined with reduce() to group objects by a key and merge their properties. This approach is useful when you have duplicate objects and want to consolidate them, such as combining student records with the same ID. Example Data Consider this collection of student objects with duplicates: var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: ...

Read More

Transform tree from DB format to JSON format in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 519 Views

When working with database records, data is often stored in a flat structure with parent-child relationships defined by IDs. This tutorial shows how to transform such flat data into a hierarchical tree structure in JavaScript. The Problem Suppose we have an array of objects representing geographical regions from a database: const arr = [ {"id":7, "name":"Kuwait", "parentId":2}, {"id":4, "name":"Iraq", "parentId":2}, {"id":10, "name":"Qatar", "parentId":2}, {"id":2, "name":"Middle East", "parentId":1}, {"id":3, "name":"Bahrain", "parentId":2}, {"id":6, "name":"Jordan", ...

Read More

Counting the occurrences of JavaScript array elements and put in a new 2d array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 483 Views

We are required to write a JavaScript function that takes in an array of literal values. The function should then count the frequency of each element of the input array and prepare a new array on that basis. For example − If the input array is − const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; Then the output should be − const output = [ [5, 3], [2, 5], [9, 1], [4, 1] ]; ...

Read More

Magical String: Question in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 559 Views

A magical string consists of only '1' and '2' characters and has a unique property: the sequence of consecutive character group lengths generates the string itself. Understanding the Magical String The magical string starts as "1221121221221121122..." where grouping consecutive characters reveals the pattern: Original: 1 22 11 2 1 22 1 22 11 2 11 22 ...... Lengths: 1 2 2 1 1 2 1 2 2 1 2 2 ...... Notice how the length sequence (1, 2, 2, 1, 1, 2, 1, 2, 2, 1, ...

Read More

How to Create an Analog Clock using HTML, CSS, and JavaScript?

Kalyan Mishra
Kalyan Mishra
Updated on 15-Mar-2026 4K+ Views

In this tutorial, we will create a functional analog clock using HTML, CSS, and JavaScript. The clock will display the current time with three hands representing hours, minutes, and seconds that move in real-time. Approach Create the clock structure using HTML with three div elements for the hands Style the clock face and hands using CSS positioning and transforms Use JavaScript's Date object to get current time and calculate hand rotations Update the clock every second using setInterval() Understanding the Time Calculations ...

Read More
Showing 14381–14390 of 61,297 articles
Advertisements