Javascript Articles

Page 123 of 534

Group all the objects together having the same value for the '_id' key in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 399 Views

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 More

How to create a third object from two objects using the key values in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 248 Views

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 More

Get intersection between two ranges in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 884 Views

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 More

Check for a self-dividing number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 290 Views

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 More

Palindrome numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 763 Views

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 More

Compare keys & values in a JSON object when one object has extra keys in JavaScript

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

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 More

Remove duplicates from array with URL values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 480 Views

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 More

Generating combinations from n arrays with m elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 819 Views

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 More

How to create every combination possible for the contents of two arrays in JavaScript

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

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 More

Separating digits of a number in JavaScript

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

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
Showing 1221–1230 of 5,340 articles
« Prev 1 121 122 123 124 125 534 Next »
Advertisements