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 101 of 534
How to convert array into array of objects using map() and reduce() in JavaScript
Converting arrays into arrays of objects is a common task in JavaScript. This article shows how to use map() and reduce() together to transform nested arrays into structured objects. Problem Statement Suppose we have an array of arrays like this: const arr = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ]; We need to convert this nested array structure into ...
Read MoreGenerate Ranking with combination of strings in JavaScript
We are required to write a JavaScript function that takes in any number of arrays of numbers and generates a frequency map of all possible combinations within each array. The function counts how many times each element and combination appears across all arrays. For example, if we have the following arrays: const a = [23, 45, 21], b = [45, 23], c = [21, 32], d = [23], e = [32], f = [50, 54]; The function should ...
Read MoreAll combinations of sums for array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n as the second argument. The number n will always be less than or equal to the length of the array. Our function should return an array of the sum of all elements of all the possible subarrays of length n from the original array. For example, If the input is: const arr = [2, 6, 4]; const n = 2; Then the output should be: const output = ...
Read MoreDynamic Programming: return all matched data in JavaScript
Dynamic programming in JavaScript can be used to efficiently search and retrieve data from nested structures. This article demonstrates how to search for all matching cities in a complex JSON object containing country and province information. The Data Structure We have a nested JSON object with countries, provinces, and cities: const countryInfo = { country: [{ name: "Bangladesh", province: [{ name:"Dhaka", ...
Read MoreCalculating quarterly and yearly average through JavaScript
In this tutorial, we'll learn how to calculate quarterly and yearly averages from an array of numbers using JavaScript. This involves chunking data into groups and computing their averages. Suppose we have an array of numbers like this: const arr = [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; console.log("Original array:", arr); Original array: [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] We need to group this array ...
Read MoreGroup strings starting with similar number in JavaScript
In JavaScript, you can group strings that start with the same number by extracting the first part before the decimal and comparing consecutive elements. This is useful when working with version numbers, decimal classifications, or any structured string data. Problem Statement Given an array of number strings like this: const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"]; console.log("Input array:", arr); Input array: [ '1.1', '1.2', '1.3', '2.1', '2.2', '3.1', '3.2', '3.3', '4.1', '4.2' ] We need to group all strings starting with the same number into ...
Read MoreReplace all occurrence of specific words in a sentence based on an array of words in JavaScript
We are required to write a JavaScript function that takes a string and an array of strings. Our function should return a new string, where all the occurrences of the word in the string that are present in the array are replaced by a whitespace. Our function should use the String.prototype.replace() method to solve this problem. Understanding the Problem When filtering words from a sentence, we need to: Match whole words only (not parts of words) Handle case-insensitive matching Use regular expressions ...
Read MoreHow to merge two strings alternatively in JavaScript
In JavaScript, merging two strings alternatively means combining characters from both strings one by one. This creates a new string where characters alternate between the first and second input strings. For example, if we have two strings: const str1 = 'abc'; const str2 = 'def'; console.log('String 1:', str1); console.log('String 2:', str2); String 1: abc String 2: def The expected output should be: adbecf Using Loop-Based Approach Here's a function that merges two strings alternatively by iterating through both strings simultaneously: const str1 = 'abc'; const ...
Read MoreRemoving duplicate objects from array in JavaScript
Removing duplicate objects from arrays is a common requirement in JavaScript applications. Let's explore different approaches to eliminate duplicate objects while preserving the original data structure. The Problem Consider an array containing duplicate objects: const arr = [ {"title": "Assistant"}, {"month": "July"}, {"event": "Holiday"}, {"title": "Assistant"} ]; console.log("Original array:", arr); Original array: [ { title: 'Assistant' }, { month: 'July' }, { event: 'Holiday' }, { title: 'Assistant' ...
Read MoreSort an array according to another array in JavaScript
When working with arrays in JavaScript, you may need to sort one array based on the order of elements in another array. This is useful when you have a reference array that defines the desired ordering. Suppose we have two arrays like these: const arr1 = [1, 3, 2, 4, 5, 6]; const arr2 = [1, 2, 5]; console.log("Original arr1:", arr1); console.log("Reference arr2:", arr2); Original arr1: [ 1, 3, 2, 4, 5, 6 ] Reference arr2: [ 1, 2, 5 ] We want to sort arr1 so that elements appearing in ...
Read More