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 123 of 534
Group all the objects together having the same value for the '_id' key in JavaScript
Suppose we have an array of objects like this − const arr = [ {_id : "1", S : "2"}, {_id : "1", M : "4"}, {_id : "2", M : "1"}, {_id : "" , M : "1"}, {_id : "3", S : "3"} ]; We are required to write a JavaScript function that takes in one such array and groups all the objects together that have the same value for the '_id' key. Therefore, the final output should ...
Read MoreHow to create a third object from two objects using the key values in JavaScript?
In JavaScript, you can create a third object from two objects by mapping key values between them. This is useful when you have one object containing arrays of keys and another object with corresponding values. Problem Statement Given two objects where the first contains arrays of keys and the second contains key-value pairs, we need to calculate totals for each category: const obj1 = { positive: ['happy', 'excited', 'joyful'], negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = { happy: 6, excited: 1, unhappy: 3 }; ...
Read MoreGet intersection between two ranges in JavaScript
In JavaScript, finding the intersection between two numerical ranges involves determining the overlapping portion. A range is typically represented as an array with two elements: [start, end]. Understanding Range Intersection The intersection of two ranges is the overlapping segment. For ranges [2, 5] and [4, 7], the intersection is [4, 5] because that's where they overlap. const arr1 = [2, 5]; const arr2 = [4, 7]; console.log("Range 1:", arr1); console.log("Range 2:", arr2); Range 1: [ 2, 5 ] Range 2: [ 4, 7 ] Algorithm To find the intersection, we ...
Read MoreCheck for a self-dividing number in JavaScript
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is self-dividing because it's divisible by 1, 2, and 8. However, any number containing 0 cannot be self-dividing since division by zero is undefined. What Makes a Number Self-Dividing? A number qualifies as self-dividing if: It contains no zeros (since division by zero is impossible) The number is evenly divisible by each of its individual digits Examples 128: Self-dividing because 128 ÷ 1 = 128, 128 ÷ 2 = ...
Read MorePalindrome numbers in JavaScript
We are required to write a JavaScript function that takes in a number and determines whether or not it is a palindrome number. Palindrome numbers − A palindrome number is that number which reads the same from both left and right sides. For example − 343 is a palindrome number 6789876 is a palindrome number 456764 is not a palindrome number Method 1: String Reversal Approach The simplest approach is to convert the number to a string, reverse it, and compare ...
Read MoreCompare keys & values in a JSON object when one object has extra keys in JavaScript
When comparing JSON objects in JavaScript, you often need to check if the common keys have matching values, even when one object has extra properties. This is useful for validating partial object matches or checking if one object is a subset of another. Problem Statement Consider these two objects: const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"}; We need a function that returns true because all common keys (a, b, c) have matching values, ignoring the extra keys (e, d) ...
Read MoreRemove duplicates from array with URL values in JavaScript
Suppose, we have an array of objects like this − const arr = [ { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello', id: "22" }, { url: 'www.example.com/hello-how-are-you', id: ...
Read MoreGenerating combinations from n arrays with m elements in JavaScript
We are required to write a JavaScript function that generates combinations from n number of arrays with m number of elements in them. For example, consider this data: const arr = [ [0, 1], [0, 1, 2, 3], [0, 1, 2] ] We have 3 sub-arrays with different numbers of elements. What we want to do is get all combinations by selecting one item from each array. Understanding the Problem The goal is to generate a Cartesian product of multiple arrays. For the example ...
Read MoreHow to create every combination possible for the contents of two arrays in JavaScript
Creating every possible combination from two arrays is known as a Cartesian product. This involves pairing each element from the first array with every element from the second array. Problem Setup Given two arrays of literals: const arr1 = ["A", "B", "C"]; const arr2 = ["1", "2", "3"]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ 'A', 'B', 'C' ] Array 2: [ '1', '2', '3' ] We need to create all possible combinations by pairing each element from the first array with each element from the second array. ...
Read MoreSeparating digits of a number in JavaScript
In JavaScript, separating digits of a number is a common programming task. This tutorial shows how to extract and display each digit of a number individually using different approaches. Problem Statement We need to create a program that takes a number as input and displays each digit separately. For example, if the input is 43354, the output should be: 4 3 3 5 4 Method 1: Using Mathematical Operations This approach uses the modulo operator (%) and integer division to extract digits from right to left: ...
Read More