Object Oriented Programming Articles

Page 181 of 589

Converting array to object by splitting the properties - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 299 Views

We have an array of string literals in which each element has a dash (-). The property key is present to the left of dash and its value to the right. A sample input array would look something like this: const arr = ["playerName-Kai Havertz", "age-21", "nationality-German", "position-CAM", "languages-German, English, Spanish", "club-Chelsea"]; We are required to write a function that splits these strings and form an object out of this array. Let's write the code. It will loop over the array splitting each string and feeding it into the new object. Using forEach Method ...

Read More

Flattening an array with truthy/ falsy values without using library functions - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 193 Views

We need to write a JavaScript function that takes a nested array containing falsy values and returns a completely flattened array. This means converting a multi-dimensional array into a single-dimensional array while preserving all elements, including falsy values like false, null, and 0. For example, if the input is: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; The output should be: [1, 2, 3, 4, 5, 5, false, 6, 5, 8, null, 6] Method 1: Using Recursive Function with Array.prototype Extension We can ...

Read More

Using recursion to remove consecutive duplicate entries from an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 561 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space. For example, if the input array is − const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; Then the output should be − const output = [17, 12, 354, 1]; Recursive Solution The recursive approach modifies the array in-place by checking consecutive elements and removing duplicates: const arr = [17, 17, 17, ...

Read More

Alternative shuffle in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 400 Views

An alternatively shuffled array in JavaScript is an array of numbers where elements are arranged such that the greatest number is followed by the smallest element, second greatest element is followed by second smallest element, and so on. For example, if we have an input array: [11, 7, 9, 3, 5, 1, 13] The alternatively shuffled output should be: [13, 1, 11, 3, 9, 5, 7] How Alternative Shuffling Works The algorithm follows these steps: Sort the array in ascending order Take the largest element from the end ...

Read More

Finding the maximum in a nested array - JavaScript

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

Let's say, we have to write a simple function in JavaScript that takes in the following array of Numbers (nested to any level) − const arr = [ 15, 24, [ 29, 85, 56, [ 36, 14, 6, 98, 34, 52 ], 22 ], 87, 60 ]; and return the greatest number present in ...

Read More

How to merge an array with an object where values are arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 218 Views

In JavaScript, you can merge an array with an object containing array values by mapping array elements to object keys sequentially. This creates a new structure where each group contains key-value pairs. Problem Statement Given an array of values and an object with arrays as values, we want to distribute the array values across the object's arrays as keys: const arr = [1, 2, 3, 4, 5]; const obj = { group1: ["Ram", "Mohan", "Shyam"], group2: ["Jai", "Dinesh"], }; // Expected output: const expected = { ...

Read More

Convert a string to hierarchical object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 502 Views

Let's say, we have a special kind of string that contains characters in couples, like this: const str = "AABBCCDDEE"; console.log(str); AABBCCDDEE We are required to construct an object based on this string which should look like this: const obj = { code: "AA", sub: { code: "BB", sub: { code: "CC", ...

Read More

How to run a function after two async functions complete - JavaScript

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

When working with multiple asynchronous operations in JavaScript, you often need to wait for all of them to complete before executing a final function. This is a common scenario in web development where you might be fetching data from multiple APIs or processing several files concurrently. There are several approaches to handle this challenge, with Promise.all() being the most efficient for running multiple async operations concurrently. Using Promise.all() (Recommended) Promise.all() executes multiple promises concurrently and waits for all of them to resolve. It's the optimal choice when you need all operations to complete regardless of their individual ...

Read More

Splitting an array based on its first value - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 392 Views

Suppose we have an array of arrays of numbers like this: const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; console.log(arr); [ [ 1, 45 ], [ 1, 34 ], [ 1, 49 ], [ 2, 34 ], [ 4, 78 ], [ 2, 67 ], [ 4, 65 ] ] Each subarray contains exactly two elements. We need to write a function that constructs a new array where all second elements of subarrays with the same first value are grouped together. For the ...

Read More

Splitting strings based on multiple separators - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 667 Views

We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified. For example, if the string is: const str = 'rttt.trt/trfd/trtr, tr'; And the separators are: const sep = ['/', '.', ', ']; Then the output should be: const output = ['rttt', 'trt', 'trfd', 'trtr']; Method 1: Using Manual Iteration This approach manually iterates through each character and checks if ...

Read More
Showing 1801–1810 of 5,881 articles
« Prev 1 179 180 181 182 183 589 Next »
Advertisements