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
Front End Technology Articles
Page 185 of 652
Building frequency map of all the elements in an array JavaScript
Building a frequency map (also called a frequency counter) is a common programming task that helps count how many times each element appears in an array. This technique is useful for data analysis, finding duplicates, or solving algorithmic problems. A frequency map returns an object where each unique element from the array becomes a key, and its corresponding value represents how many times that element appears. Basic Approach Using forEach() The most straightforward method is to iterate through the array and build an object that tracks the count of each element: const arr = [2, ...
Read MoreZig-Zag pattern in strings in JavaScript?
We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string. Understanding the Pattern A zig-zag pattern alternates between lowercase and uppercase characters based on their index position. Even indices (0, 2, 4...) become lowercase, while odd indices (1, 3, 5...) become uppercase. Example Implementation const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str ...
Read MoreFetching object keys using recursion in JavaScript
When working with nested objects in JavaScript, you often need to search for specific keys at any level of nesting. Recursion provides an elegant solution for traversing these complex data structures. The Problem Consider a nested object structure where we need to find all values for a specific key across all levels: const people = { Ram: { fullName: 'Ram Kumar', details: { age: ...
Read MoreWeight sum of a nested array in JavaScript
We are required to write a JavaScript function that takes in a nested array, arr (nested up to any level) as the only argument. The function should calculate the weighted sum of the nested array and return that sum. For calculating weighted sum, we multiply each element with its level of nesting and add throughout the array. Elements at the first level are multiplied by 1, second level by 2, and so on. Problem Example For example, if the input to the function is: const arr = [4, 7, [6, 1, [5, 2]]]; ...
Read MoreHow to remove some items from array when there is repetition in JavaScript
In JavaScript, you may need to remove items from an array that appear in multiples of three (triplets). This is useful when you want to keep only the remaining elements after removing complete sets of triplets. Understanding the Problem The goal is to remove triplets (groups of 3 identical elements) from an array and keep only the remaining elements. For example, if an array has 5 occurrences of the number 1, we remove 3 of them (one triplet) and keep 2. Solution Implementation const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = ...
Read MoreConverting array of Numbers to cumulative sum array in JavaScript
We have an array of numbers like this: const arr = [1, 1, 5, 2, -4, 6, 10]; We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point (cumulative sum). Therefore, the output should look like: const output = [1, 2, 7, 9, 5, 11, 21]; Let's explore different approaches to create a cumulative sum array. Using forEach() Method We can iterate through the array and build the cumulative sum by adding each element ...
Read MoreGroup by element in array JavaScript
Grouping array elements by a specific property is a common JavaScript task. This tutorial shows how to group an array of objects by their uuid property into separate sub-arrays. Problem Statement Given an array of objects with similar properties, we need to group them by a specific key and return an array of arrays. const arr = [ {"name": "toto", "uuid": 1111}, {"name": "tata", "uuid": 2222}, {"name": "titi", "uuid": 1111} ]; console.log("Original array:", arr); Original array: [ { name: ...
Read MoreHow to convert array of decimal strings to array of integer strings without decimal in JavaScript
We are required to write a JavaScript function that takes in an array of decimal strings. The function should return an array of strings of integers obtained by flooring the original corresponding decimal values of the array. For example, If the input array is − const input = ["1.00", "-2.5", "5.33333", "8.984563"]; Then the output should be − const output = ["1", "-2", "5", "8"]; Method 1: Using parseInt() The parseInt() function parses a string and returns an integer. It automatically truncates decimal parts. const input = ["1.00", ...
Read MoreHow 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 More