Front End Technology Articles

Page 219 of 652

Transforming array to object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 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 616 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 472 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 611 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 346 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 397 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 524 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

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 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

Creating an array of objects based on another array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 739 Views

In JavaScript, you can create a new array of objects based on an existing array by transforming each object's structure. This is commonly done using the map() method to extract specific properties and create new object formats. Suppose we have an array of objects containing user data like this: const arr = [ {"user":"dan", "liked":"yes", "age":"22"}, {"user":"sarah", "liked":"no", "age":"21"}, {"user":"john", "liked":"yes", "age":"23"}, ]; console.log("Original array:", arr); Original array: [ { user: 'dan', liked: 'yes', age: '22' }, { ...

Read More
Showing 2181–2190 of 6,519 articles
« Prev 1 217 218 219 220 221 652 Next »
Advertisements