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 233 of 801
Most efficient method to groupby on an array of objects - JavaScript
Grouping arrays of objects is a common task in JavaScript data processing. We'll explore efficient methods to group objects by single or multiple properties and aggregate values. Sample Data Let's start with an array of objects representing project phases, steps, and tasks: const arr = [ { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" }, { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" }, { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" ...
Read MoreGet the smallest array from an array of arrays in JavaScript
When working with nested arrays in JavaScript, you might need to find the smallest subarray based on the number of elements. This is useful for data processing, filtering operations, or when you need to identify the shortest path or sequence. Suppose we have a nested array of arrays like this: const arr = [ ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"], ["RIGHT", "LEFT", "TOP"], ["TOP", "LEFT"] ]; console.log("Original array:", arr); Original array: [ [ 'LEFT', 'RIGHT', 'RIGHT', 'BOTTOM', 'TOP' ], ...
Read MoreSorting an array by price in JavaScript
Sorting an array of objects by price is a common requirement in JavaScript applications. This article demonstrates how to sort house data by price values stored as strings. Sample Data Let's start with an array of house objects where prices are stored as strings: const arr = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", ...
Read MoreSort by index of an array in JavaScript
Sorting an array of objects by a specific property is a common task in JavaScript. This tutorial demonstrates how to sort objects by their index property and extract specific values. Problem Statement Suppose we have the following array of objects: const arr = [ { 'name': 'd', 'index': 3 }, { 'name': 'c', ...
Read MoreTurning a 2D array into a sparse array of arrays in JavaScript
Suppose, we have a 2-D array like this − const arr = [ [3, 1], [2, 12], [3, 3] ]; We are required to write a JavaScript function that takes in one such array. The function should then create a new 2-D array that contains all elements initialized to undefined other than the element's index present in the input array. Therefore, for the input array: output[3][1] = 1; output[2][12] = 1; output[3][3] = 1; And rest all elements ...
Read MoreAlphanumeric sorting using JavaScript
We have a mixed array that we need to sort by alphabet and then by digit. This requires a custom sorting function that handles both alphabetical and numerical parts correctly. const arr = ['Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105']; console.log("Original array:", arr); Original array: [ 'Ab-1', 'Ab-11', 'Ab-12', 'ab-10', 'ab-100', 'ab-101', 'ab2', 'ab-3', 'ab-105' ] The Problem with Standard Sort JavaScript's default string sort doesn't handle numbers correctly. It treats "10" as smaller than "2" because it compares character ...
Read MoreEnter a number and write a function that adds the digits together on button click in JavaScript
We are required to write a JavaScript program that provides users an input to fill in a number. And upon filling when the user clicks the button, we should display the sum of all the digits of the number. HTML Structure First, let's create the HTML elements needed for our application: Submit JavaScript Implementation Here's the JavaScript function that calculates the sum of digits: function myFunc() { var num = document.getElementById('digits').value; var tot = 0; ...
Read MoreJavaScript - summing numbers from strings nested in array
When working with arrays containing string numbers (like credit card numbers), you often need to extract and sum the numeric values. This article shows how to find the string with the greatest sum of digits. Problem Statement Given an array of credit card numbers as strings, we need to find the card with the highest sum of digits. If multiple cards have the same sum, return the last one encountered. const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; Solution Approach The solution involves two main steps: extracting numbers from each string and calculating their ...
Read MoreLongest decreasing subsequence subarray in JavaScript
We are required to write a JavaScript function that takes in an array of integers and returns the length of the longest consecutive decreasing subsequence (subarray) from the array. Problem Statement Given an array of integers, find the length of the longest contiguous subsequence where each element is smaller than the previous one. For example, if the input array is: const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7]; The longest decreasing subsequence is [5, 4, 3, 2] with length 4. Solution Approach We'll track the current decreasing ...
Read MoreFuzzy Search Algorithm in JavaScript
Fuzzy search allows finding strings that match a pattern even when characters are not consecutive. In JavaScript, we can implement a fuzzy search algorithm that checks if characters from a search query appear in the same order within a target string. How Fuzzy Search Works The algorithm loops through each character of the search query and verifies that all characters exist in the target string in the same sequential order, though they don't need to be adjacent. For example: ('a haystack with a needle').fuzzySearch('hay sucks'); // false ('a haystack with a needle').fuzzySearch('sack hand'); // true ...
Read More