Web Development Articles

Page 220 of 801

Sort array according to the date property of the objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

In JavaScript, sorting an array of objects by date requires converting date strings to Date objects for proper comparison. The Array.sort() method with a custom comparator function handles this efficiently. Sample Data Consider an array of objects with date properties: const arr = [ {date: "2016-06-08 18:10:00"}, {date: "2016-04-26 20:01:00"}, {date: "2017-02-06 14:38:00"}, {date: "2017-01-18 17:30:21"}, {date: "2017-01-18 17:24:00"} ]; Using Array.sort() with Date Comparison The most straightforward approach uses Array.sort() with a ...

Read More

Dynamic Programming: Is second string subsequence of first JavaScript

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

We are given two strings str1 and str2, we are required to write a function that checks if str1 is a subsequence of str2. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. For example, "ace" is a subsequence of "abcde" while "aec" is not because the relative order is not maintained. Understanding Subsequences In a subsequence, characters must appear in the same order as in the original string, but they ...

Read More

Transforming array to object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 Views

Suppose we have an array of strings like this − const arr = [ 'type=A', 'day=45' ]; We are required to write a JavaScript function that takes in one such array. The function should construct an object based on this array. The object should contain a key/value pair for each string in the array. For any string, the part before '=' becomes the key and the part after it becomes the value. Method 1: Using for Loop const arr = [ 'type=A', 'day=45' ]; const arrayToObject = (arr = []) => ...

Read More

How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 611 Views

When working with flattened objects that use dot notation (like "car.make" or "visits.0.date"), you often need to convert them back into nested objects and arrays. This process is called "unflattening" and is useful when working with form data, APIs, or database results. The Problem Suppose we have a flattened object like this: const obj = { "firstName": "John", "lastName": "Green", "car.make": "Honda", "car.model": "Civic", "car.revisions.0.miles": 10150, "car.revisions.0.code": "REV01", ...

Read More

Get all methods of any object JavaScript

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

In JavaScript, you can extract all methods (functions) from any object using Object.getOwnPropertyNames() combined with type filtering. This is useful for debugging, reflection, or dynamically working with object methods. Understanding the Problem Objects contain both properties (data) and methods (functions). To get only the methods, we need to: Get all property names from the object Filter properties where the value type is "function" Return an array of method names Using Object.getOwnPropertyNames() The Object.getOwnPropertyNames() method returns an array of all properties (enumerable and non-enumerable) found directly on the given object. We then filter this ...

Read More

Longest distance between 1s in binary JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 465 Views

We are required to write a JavaScript function that takes a positive integer n and finds the longest distance between any two adjacent 1's in its binary representation. If there are no two adjacent 1's, the function returns 0. Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. Understanding the Problem For example, in the binary representation "1001", the two 1's have a distance of 3 (positions 0 and 3). Let's examine the number 22: ...

Read More

Finding degree of subarray in an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 602 Views

The degree of an array is defined as the maximum frequency of any element in the array. Finding the shortest subarray with the same degree is a common programming problem that involves tracking element frequencies and their positions. const arr = [1, 2, 3, 3, 5, 6, 4, 3, 8, 3]; console.log("Array:", arr); console.log("Degree of array: 4 (element 3 appears 4 times)"); Array: [ 1, 2, 3, 3, 5, 6, 4, 3, 8, 3 ] Degree of array: 4 (element 3 appears 4 times) Our task is to find the length of the ...

Read More

Longest subarray which only contains strictly increasing numbers JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 339 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should then return the length of the longest continuous subarray from the array that only contains elements in a strictly increasing order. A strictly increasing sequence is the one in which any succeeding element is greater than all its preceding elements. Example const arr = [5, 7, 8, 12, 4, 56, 6, 54, 89]; const findLongest = (arr) => { if(arr.length == 0) { ...

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

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
Showing 2191–2200 of 8,010 articles
« Prev 1 218 219 220 221 222 801 Next »
Advertisements