Javascript Articles

Page 457 of 534

Recursively flat an object JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 441 Views

We are required to write a function that flattens nested objects by converting deeply nested properties into dot-notation keys. This is useful for data transformation and API processing. If the input object is: const input = { a: 0, b: {x: {y: 1, z: 2}}, c: 3 }; Then the output of the function should be: const output = { a: 0, 'b.x.y': 1, 'b.x.z': 2, c: 3 } Recursive ...

Read More

How do I write a function that takes an array of values and returns an object JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 617 Views

Let's say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types. For example − // if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' ...

Read More

Split one-dimensional array into two-dimensional array JavaScript

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

We are required to write a function that takes in a one-dimensional array as the first argument and a number n as the second argument and we have to make n subarrays inside of the parent array (if possible) and divide elements into them accordingly. If the array contains 9 elements and we asked to make 4 subarrays, then dividing 2 elements in each subarray creates 5 subarrays and 3 in each creates 3, so in such cases we have to fallback to nearest lowest level (3 in ...

Read More

Sort array based on another array in JavaScript

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

We often need to sort an array based on the order specified in another array. This technique is useful when you want certain elements to appear first while maintaining the original order of remaining elements. For example, we have an original array and want elements from a sortOrder array to appear at the beginning: const originalArray = ['Apple', 'Cat', 'Fan', 'Goat', 'Van', 'Zebra']; const sortOrder = ['Zebra', 'Van']; Using Custom Sort Function We can create a custom comparator function that prioritizes elements present in the sortOrder array: const originalArray = ['Apple', 'Cat', ...

Read More

Filter an object based on an array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 833 Views

Let's say we have an array and an object like this: const arr = ['a', 'd', 'f']; const obj = { "a": 5, "b": 8, "c": 4, "d": 1, "e": 9, "f": 2, "g": 7 }; We are required to write a function that takes in the object and the array and filter away all the object properties that are not an element of the array. So, the output should only contain 3 properties, namely: "a", "d" and "f". Method 1: ...

Read More

What should be the correct Algorithm to Get Array B from Array A counting backwards from the last element in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 140 Views

Consider the following binary array (Array A) − const arr = [1, 0, 1, 1, 1, 1, 0, 1, 1]; When this array is passed through the function, say sumRight(), it produces the following output array (Array B) − const output = [1, 0, 4, 3, 2, 1, 0, 2, 1]; Understanding the Algorithm Elements in array arr can be either 0 or 1. The function counts backward from the last element of array arr, if there are consecutive 1's in the array arr then the corresponding element in the output ...

Read More

JavaScript Return the lowest index at which a value should be inserted into an array once it has been sorted (either in ascending or descending order).

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

We have to write a function that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted (either in ascending or descending order). The returned value should be a number. For example, Let's say, we have a function getIndexToInsert() − getIndexToInsert([1, 2, 3, 4], 1.5, 'asc') should return 1 because it is greater than 1 (index 0), but less than 2 (index 1). Likewise, getIndexToInsert([20, 3, 5], 19, 'asc') should return 2 because once the array has been sorted in ...

Read More

How to remove every Nth element from an array JavaScript?

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

Removing every Nth element from an array is a common task in JavaScript. This can be accomplished using the Array.prototype.splice() method to modify the array in-place, or by creating a new array with the filtered elements. Method 1: Using splice() (In-Place Modification) The splice() method removes elements from the array and modifies the original array: const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; console.log("Original array:", arr); const removeNth = (arr, n) => { // Start from n-1 index and remove every nth element ...

Read More

Extract properties from an object in JavaScript

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

We have to write a JavaScript function, say extract() that extracts properties from an object to another object and then deletes them from the original object. For example − If obj1 and obj2 are two objects, then obj1 = {color:"red", age:"23", name:"cindy"} obj2 = extract(obj1, ["color", "name"]) After passing through the extract function, they should become like − obj1 = { age:23 } obj2 = {color:"red", name:"cindy"} Therefore, let's write the code for this function − Syntax const extract = (obj, ...keys) => { ...

Read More

Convert an array of binary numbers to corresponding integer in JavaScript

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

Let's say, we have an array of Numbers that contains 0 and 1 − const arr = [0, 1, 0, 1]; We are required to write an Array function, toBinary() that returns the corresponding decimal integer for the array it is used with. For example − If the array is − const arr = [1, 0, 1, 1]; Then the output should be 11 because the decimal representation of binary 1011 is 11. Therefore, let's write the code for this function. Method 1: Using parseInt() with join() In ...

Read More
Showing 4561–4570 of 5,340 articles
« Prev 1 455 456 457 458 459 534 Next »
Advertisements