AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 282 of 840

Difference between two times using Dayjs JavaScript library?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 555 Views

Day.js is a lightweight JavaScript library for date manipulation. The diff() method calculates the difference between two time instances and returns the result in the specified unit. Syntax dayjs().diff(date, unit) Parameters date - The date to compare with unit - The unit of measurement: 'milliseconds', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years' Example: Basic Time Difference Day.js Time Difference ...

Read More

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

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 708 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 518 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 482 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

Creating permutations to reach a target number, but reuse the provided numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a target sum Number as the second argument. The function should return an array of all those subarrays from the original array whose elements sum to make the target sum. We can use a single number multiple times to achieve the sum. For example − If the input array and number are − const arr = [1, 2, 4]; const sum = 4; then the output should be − [ ...

Read More

Transform data from a nested array to an object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 844 Views

Suppose, we have the following array of arrays: const arr = [ [ ['dog', 'Harry'], ['age', 2] ], [ ['dog', 'Roger'], ['age', 5] ] ]; We are required to write a JavaScript function that takes in one such nested array. The function should then prepare an object based on the array. The object for the above array should look like: const output = [ {dog: 'Harry', ...

Read More
Showing 2811–2820 of 8,392 articles
« Prev 1 280 281 282 283 284 840 Next »
Advertisements