Web Development Articles

Page 233 of 801

Most efficient method to groupby on an array of objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 333 Views

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 More

Get the smallest array from an array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 552 Views

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 More

Sorting an array by price in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

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 More

Sort by index of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

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 More

Turning a 2D array into a sparse array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 266 Views

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 More

Alphanumeric sorting using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 483 Views

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 More

Enter a number and write a function that adds the digits together on button click in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 665 Views

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 More

JavaScript - summing numbers from strings nested in array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 229 Views

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 More

Longest decreasing subsequence subarray in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

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 More

Fuzzy Search Algorithm in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

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
Showing 2321–2330 of 8,010 articles
« Prev 1 231 232 233 234 235 801 Next »
Advertisements