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 449 of 534
Combine unique items of an array of arrays while summing values - JavaScript
We have an array of arrays, where each subarray contains exactly two elements: a string (person name) and an integer (value). Our goal is to combine subarrays with the same first element and sum their second elements. For example, this input array: const example = [ ['first', 12], ['second', 19], ['first', 7] ]; Should be converted to: [ ['first', 19], ['second', 19] ] Solution Using Object Mapping We'll create ...
Read MoreFix Problem with .sort() method in JavaScript, two arrays sort instead of only one
One property of the Array.prototype.sort() function is that it is an in-place sorting algorithm, which means it does not create a new copy of the array to be sorted, it sorts the array without using any extra space, making it more efficient and performant. But this characteristic sometimes leads to an awkward situation. Let's understand this with an example. Assume, we have a names array with some string literals. We want to keep the order of this array intact and want another array containing the same elements as the names array but sorted alphabetically. We can do something ...
Read MoreJavaScript : Why does % operator work on strings? - (Type Coercion)
Let's say we have a code snippet that produces some surprising results. First, the modulo operator works with strings (unexpectedly). Second, concatenation of two strings produces awkward results. We need to explain why JavaScript behaves this way through type coercion. Problem Code const numStr = '127'; const result = numStr % 5; const firstName = 'Armaan'; const lastName = 'Malik'; const fullName = firstName + + lastName; console.log('modulo result:', result); console.log('full name:', fullName); modulo result: 2 full name: ArmaanNaN What is Type Coercion? Type coercion is JavaScript's automatic conversion of ...
Read MoreMerge object properties through unique field then print data - JavaScript
Let's say we have a students object containing two properties names and marks. The names is an array of objects with each object having two properties name and roll, similarly marks is an array of objects with each object having properties mark and roll. Our task is to combine the marks and names properties according to the appropriate roll property of each object. Problem Statement The students object is given here: const students = { marks: [{ roll: 123, ...
Read MoreAdding two arrays of objects with existing and repeated members of two JavaScript arrays replacing the repeated ones
When working with arrays of objects in JavaScript, you often need to merge them while avoiding duplicates. This tutorial shows how to combine two arrays of objects and remove duplicates based on a specific property. The Problem Consider two arrays of objects where some objects have the same name property. We want to merge them into one array, keeping only unique entries based on the name field. const first = [{ name: 'Rahul', age: 23 }, { name: 'Ramesh', age: ...
Read MoreHow to find the one integer that appears an odd number of times in a JavaScript array?
We are given an array of integers and told that all the elements appear for an even number of times except a single element. Our job is to find that element in single iteration. Let this be the sample array: [1, 4, 3, 4, 2, 3, 2, 7, 8, 8, 9, 7, 9] Understanding XOR Operator Before attempting this problem, we need to understand a little about the bitwise XOR (^) operator. The XOR operator returns TRUE if both the operands are complementary to each other and returns FALSE if both the operands ...
Read MoreJavaScript array.includes inside nested array returning false where as searched name is in array
When working with nested arrays in JavaScript, the standard includes() method only checks the first level of the array. This article explores why this happens and provides a simple solution using JSON.stringify() to search through multidimensional arrays. The Problem The Array.prototype.includes() method only performs shallow comparison, meaning it cannot find elements nested within sub-arrays: const names = ['Ram', 'Shyam', ['Laxman', 'Jay']]; console.log(names.includes('Ram')); // true - found at first level console.log(names.includes('Laxman')); // false - nested inside sub-array true false Solution: Using JSON.stringify() A simple approach ...
Read MoreFinding out the Harshad number JavaScript
Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9. All single digit numbers are harshad numbers. Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012]. Our job is to write a function that takes in ...
Read MoreCreate a polyfill to replace nth occurrence of a string JavaScript
A polyfill is a piece of code that provides functionality that isn't natively supported. In this tutorial, we'll create a polyfill to remove the nth occurrence of a substring from a string in JavaScript. Problem Statement We need to create a polyfill function removeStr() that extends the String prototype. The function should: subStr → the substring whose nth occurrence needs to be removed num → the occurrence number to remove (1st, 2nd, 3rd, etc.) Return the modified string if successful, or -1 if the nth occurrence doesn't exist ...
Read MoreValidate input: replace all 'a' with '@' and 'i' with '!'JavaScript
We need to write a function validate() that takes a string as input and returns a new string where all occurrences of 'a' are replaced with '@' and all occurrences of 'i' are replaced with '!'. This is a classic string manipulation problem that can be solved using different approaches. Let's explore the most common methods. Using For Loop The traditional approach iterates through each character and builds a new string: const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => { let validatedString = ''; ...
Read More