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 107 of 534
Checking the equality of array elements (sequence dependent) in JavaScript
In this article, we'll learn how to check the equality of array elements in a sequence-dependent manner. This means comparing arrays element by element at the same index positions to find matching values or count similarities. Input-Output Scenario Let's look at how sequence-dependent comparison works with two arrays: Array1 = [2, 5, 6, 7, 9, 0]; Array2 = [3, 5, 0, 8, 9, 4]; Output = [5, 9] In the above example, elements 5 and 9 appear at the same index positions (index 1 and 4) in both arrays, making them sequence-dependent matches. ...
Read MoreGrouping array of array on the basis of elements in JavaScript
When working with arrays of arrays in JavaScript, you might need to group elements based on specific criteria. This article demonstrates how to group the second elements of subarrays that share the same first element. Problem Statement Suppose we have an array of arrays where each subarray contains exactly two elements: const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; console.log("Input array:", arr); Input array: [ [ 1, 45 ], [ 1, 34 ], [ 1, 49 ], [ 2, 34 ], [ 4, 78 ...
Read MoreReverse mapping an object in JavaScript
Reverse mapping an object in JavaScript involves swapping the keys and values of an object. This technique is useful when you need to look up keys based on their values. Suppose we have an object like this: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; All the keys are unique in themselves and all the values are unique in themselves. We need to write a function that accepts a value and returns its corresponding key. Method 1: Using Object.keys() with map() ...
Read MoreAdd matching object values in JavaScript
In JavaScript, you can add matching object values by iterating through an array of objects and accumulating values for common keys. This is useful for aggregating data from multiple objects. Consider an array of objects like this: const arr = [{a: 2, b: 5, c: 6}, {a: 3, b: 4, d: 1}, {a: 1, d: 2}]; console.log("Input array:", arr); Input array: [ { a: 2, b: 5, c: 6 }, { a: 3, b: 4, d: 1 }, { a: 1, d: 2 } ] Each object has unique properties within itself, ...
Read MoreSumming up unique array values in JavaScript
We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array. Problem Understanding If the input array is: const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; The unique elements (appearing only once) are: 3, 7, 4, 11. Their sum is 3 + 7 + 4 + 11 = 25. Using indexOf() and lastIndexOf() We ...
Read MoreAbsolute difference of Number arrays in JavaScript
In JavaScript, calculating the absolute difference between corresponding elements of two arrays is a common operation. This involves subtracting elements at the same index positions and taking the absolute value of the result. Suppose we have two arrays like these: const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We need to create a function that returns an array of absolute differences between corresponding elements. For these arrays, the expected output should be: [8, 6, 4, 1, 3, 3] Using For Loop ...
Read MoreSort the second array according to the elements of the first array in JavaScript
Suppose, we have two arrays like these − const arr1 = ['d', 'a', 'b', 'c']; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}]; console.log("First array:", arr1); console.log("Second array:", arr2); First array: [ 'd', 'a', 'b', 'c' ] Second array: [ { a: 1 }, { c: 3 }, { d: 4 }, { b: 2 } ] We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array. We have to sort the keys of ...
Read MoreRemoving all the empty indices from array in JavaScript
When working with JavaScript arrays, you may encounter arrays with empty indices (holes). These empty slots can cause issues in data processing. This article shows different methods to remove empty indices and create clean arrays. Understanding Empty Indices Empty indices in arrays are undefined slots that exist but have no value assigned: Empty Indices Example const array = [22, 45, , 56, 71, , 10]; console.log("Original array:", array); ...
Read MoreFunction to compute factorial of a number in JavaScript
In this article, we will explore different methods to calculate the factorial of a number in JavaScript. What is Factorial? The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. For example, the factorial of 4 is 4 × 3 × 2 × 1 = 24, represented as 4!. The value of 0! is defined as 1 by convention. Mathematical Formula The factorial of n is represented as n! and calculated using: n! = n × (n-1) × (n-2) × ... × 3 ...
Read MoreReturn an array of all the indices of minimum elements in the array in JavaScript
In JavaScript arrays, you might need to find all indices where the minimum value appears. This is useful when the smallest element occurs multiple times and you want to locate all positions. Problem Statement Given an array with duplicate minimum values, we need to return an array containing all indices of these minimum elements. Input: [10, 22, 30, 44, 10, 22, 30, 10, 10] Output: [0, 4, 7, 8] Here, 10 is the minimum value appearing at indices 0, 4, 7, and 8. Using Math.min() with Spread Operator Math.min() returns the smallest ...
Read More