Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Javascript Articles
Page 482 of 534
Finding the difference between two arrays - JavaScript
Finding the difference between two arrays means identifying elements that exist in one array but not in the other. This is a common operation when comparing datasets or finding unique values. We have two arrays of numbers like these: const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; We need to write a JavaScript function that takes in two such arrays and returns the elements that are not common to both arrays. Using indexOf() Method The traditional approach uses nested loops ...
Read MoreJavaScript - Writing a string function to replace the kth appearance of a character
Let's say, we are required to write a String.prototype function that takes in three arguments. First argument is string that should be searched for substrings Second argument is the string, the occurrence of which String to be removed Third argument is a Number say n, nth occurrence of substring to be removed from string. The function should return the new string if the removal of the subStr from the string was successful, otherwise it should return -1 in all cases. Example Following is the code − const str = 'jkdsttjkdsre'; const subStr ...
Read MoreRemoving redundant elements from array altogether - JavaScript
We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array. For example, if the input is: const arr = [763, 55, 43, 22, 32, 43, 763, 43]; The output should be: const output = [55, 22, 32]; Understanding the Approach We will be using the following two methods: ...
Read MoreConverting array to object by splitting the properties - JavaScript
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 MoreFlattening an array with truthy/ falsy values without using library functions - JavaScript
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 MoreUsing recursion to remove consecutive duplicate entries from an array - JavaScript
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 MoreAlternative shuffle in JavaScript
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 MoreFinding the maximum in a nested array - JavaScript
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 MoreHow to merge an array with an object where values are arrays - JavaScript
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 MoreConvert a string to hierarchical object - JavaScript
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