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
Web Development Articles
Page 224 of 801
Sum of array object property values in new array of objects in JavaScript
When working with arrays of objects, we often need to group objects by a common property and sum up their numeric values. This is particularly useful when dealing with student records, sales data, or any scenario where duplicate categories need to be consolidated. Suppose we have an array of objects that contains data about students and their marks: const arr = [ { subject: 'Maths', marks: '40', noOfStudents: '5' }, { subject: 'Science', marks: '50', noOfStudents: '16' }, { subject: 'History', marks: '35', noOfStudents: '23' }, { subject: 'Science', ...
Read MoreCalculating Josephus Permutations efficiently in JavaScript
This problem takes its name from arguably the most important event in the life of the ancient historian Josephus. According to his tale, he and his 40 soldiers were trapped in a cave by the Romans during a siege. Refusing to surrender to the enemy, they instead opted for mass suicide, with a twist — they formed a circle and proceeded to kill one man every three, until one last man was left (and that it was supposed to kill himself to end the act). Josephus and another man were the last two and, as we now know ...
Read MoreHow to split comma and semicolon separated string into a two-dimensional array in JavaScript ?
Let's say we have a variable "users" that contains the following string of text where each user is separated by a semicolon and each attribute of each users is separated by a comma − const users = 'Bob, 1234, Bob@example.com;Mark, 5678, Mark@example.com'; We are required to write a JavaScript function that takes in one such string and splits this into a multidimensional array that looks like this − const arr = [ ['Bob', 1234, 'Bob@example.com'], ['Mark', 5678, 'Mark@example.com'] ]; Method 1: Using split() and for Loop ...
Read MoreFinding the least common multiple of a range of numbers in JavaScript?
We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should then calculate the least common multiple of all the numbers within that range and return the final result. Understanding LCM and GCD To find the LCM of multiple numbers, we first need helper functions for GCD (Greatest Common Divisor) and LCM of two numbers: GCD: The largest number that divides both numbers evenly LCM: The smallest positive number that is divisible by both numbers Formula: LCM(a, b) = (a × b) / ...
Read MoreHow to find a value is present in binary tree or not in JavaScript ?
We are required to write a JavaScript function on the prototype object of a BinarySearchTree data type that takes in a value and finds whether or not that value is contained in the BST. Binary Search Tree Structure A Binary Search Tree (BST) is a tree data structure where each node has at most two children. The left child contains values smaller than the parent, and the right child contains values greater than the parent. 50 30 ...
Read MoreFinding word starting with specific letter in JavaScript
Finding words that start with a specific letter is a common requirement in JavaScript applications. This guide shows different approaches to locate the first array element beginning with a specified character. Problem Statement We need to write a JavaScript function that takes an array of strings and a character, then returns the index of the first string starting with that character. Using substring() Method The substring() method extracts the first character for comparison: const names = ['Naman', 'Kartik', 'Anmol', 'Rajat', 'Keshav', 'Harsh', 'Suresh', 'Rahul']; const firstIndexOf = (arr = [], char = '') ...
Read MoreMerge two sorted arrays to form a resultant sorted array in JavaScript
We are required to write a JavaScript function that takes in two sorted arrays of numbers. The function should merge the two arrays together to form a resultant sorted array and return that array. For example, if the two arrays are: const arr1 = [2, 6, 6, 8, 9]; const arr2 = [1, 4, 5, 7]; Then the output array should be: [1, 2, 4, 5, 6, 6, 7, 8, 9] Method 1: Using Two Pointers Technique The most efficient approach is the two-pointers technique, which compares elements from both ...
Read MoreHow to merge two arrays with objects in one in JavaScript?
Suppose, we have two arrays of objects like these − const arr1 = [ {name:'test', lastname: 'test', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'} ]; const arr2 = [ {name:'test21', lastname: 'test21', gender:'f'}, {name:'test1', lastname: 'test1', gender:'f'}, {name:'test2', lastname: 'test2', gender:'m'}, {name:'test22', lastname: 'test22', gender:'m'} ]; console.log("Array 1:", arr1); console.log("Array 2:", arr2); Array 1: [ { name: 'test', lastname: 'test', gender: 'f' }, { name: 'test1', lastname: 'test1', gender: 'f' }, { ...
Read MoreRegroup JSON array in JavaScript
Suppose, we have a JSON array of objects like this − const arr = [ { "id": "03868185", "month_10": 6, }, { "id": "03870584", "month_6": 2, }, { "id": "03870584", ...
Read MoreCheck how many objects are in the array with the same key in JavaScript
Suppose, we have an array of objects containing some data about some users like this − const arr = [ { "name":"aaa", "id":"2100", "designation":"developer" }, { "name":"bbb", "id":"8888", "designation":"team lead" }, { "name":"ccc", "id":"6745", ...
Read More