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 227 of 801
Group a JavaScript Array
Grouping JavaScript arrays by a specific property is a common requirement when working with complex data structures. In this tutorial, we'll learn how to group array elements by the tableName property and restructure the data. Problem Statement Suppose we have a JavaScript array with objects containing table data: const data = [ { "dataId": "1", "tableName": "table1", "column": "firstHeader", "rows": ...
Read MoreSort array based on min and max date in JavaScript?
When working with arrays of date strings, you often need to find the oldest (minimum) and newest (maximum) dates. JavaScript provides several approaches to accomplish this task efficiently. const arr = [ "2017-01-22 00:21:17.0", "2017-01-27 11:30:23.0", "2017-01-24 15:53:21.0", "2017-01-27 11:34:18.0", "2017-01-26 16:55:48.0", "2017-01-22 11:57:12.0", "2017-01-27 11:35:43.0" ]; We need to write a JavaScript function that takes such an array, finds the oldest and newest date, and returns an object containing both dates. Using Array.reduce() Method The reduce() method provides an ...
Read MoreCalculate average from JSON data based on multiple filters JavaScript
When working with JSON data, you often need to filter, group, and calculate averages based on multiple criteria. This article demonstrates how to group objects by multiple fields and compute averages while handling edge cases like undefined values. Problem Statement Given an array of supplier objects, we need to: Group objects with the same "SupplierName" and "Category" Sum their points together (ignoring undefined values) Calculate the average points for each group Return the grouped results with totals and averages ...
Read MoreCounting prime numbers from 2 upto the number n JavaScript
We are required to write a JavaScript function that takes in a number, say n, as the first and the only argument. The function should then return the count of all the prime numbers from 2 up to the number n (exclusive). For example: For n = 10, the output should be: 4 (2, 3, 5, 7) For n = 1, the output should be: 0 Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime ...
Read MoreConvert the number or boolean type JSON object from string type to its original in JavaScript
Suppose we have a JSON object where numeric and boolean values are stored as strings: const obj = {"name":"sam", "age":"24", "isMarried":"false"}; console.log(obj); { name: 'sam', age: '24', isMarried: 'false' } Here, the age property should be a number and isMarried should be a boolean. Our goal is to write a function that converts these string values back to their correct data types. Method 1: Using parseInt and Boolean Conversion This approach checks each property and converts numeric strings to numbers and boolean strings to booleans: const obj = { ...
Read MoreSearch from an array of objects via array of string to get array of objects in JavaScript
Filtering an array of objects based on keys from another array is a common requirement in JavaScript. This tutorial shows how to extract matching objects using various approaches. Problem Statement Given an array of strings and an array of objects, we need to filter objects whose KEY property exists in the string array: const searchKeys = ['1956888670', '2109171907', '298845084']; const users = [ { KEY: '1262875245', VALUE: 'Vijay Kumar Verma' }, { KEY: '1956888670', VALUE: 'Sivakesava Nallam' }, { KEY: '2109171907', VALUE: 'udm analyst' ...
Read MoreConvert array with duplicate values to object with number of repeated occurrences in JavaScript
When working with arrays that contain duplicate values, you often need to count occurrences and convert them into a structured format. This article shows how to transform an array with duplicates into an array of objects containing each unique value and its count. Problem Statement Suppose we have an array with duplicate entries like this: const arr = ['California', 'Texas', 'Texas', 'Texas', 'New York', 'Missouri', 'New Mexico', 'California']; console.log("Original array:", arr); Original array: [ 'California', 'Texas', 'Texas', 'Texas', 'New York', ...
Read MoreFilter nested object by keys using JavaScript
When working with arrays of objects in JavaScript, you often need to filter them based on specific criteria. This article demonstrates how to filter an array of objects by checking if their title property contains any of the specified search terms. Problem Statement Suppose we have an array of objects like this: const arr = [{ 'title': 'Hey', 'foo': 2, 'bar': 3 }, { 'title': 'Sup', 'foo': 3, 'bar': 4 }, { ...
Read MoreJavaScript - Determine all possible ways a group of values can be removed from a sequence
We are required to write a JavaScript function that determines how many different ways we can remove a group of values from a sequence, leaving the original sequence in order (stable), and making sure to remove only one instance of each value from the original sequence. For example − If the sequence array is − const arr = [1, 2, 1, 3, 1, 4, 4]; And the array to be removed is − const arr2 = [1, 4, 4]; Then there are three possible ways of doing this without disrupting the ...
Read MoreSort array by month-year JavaScript
Suppose, we have an array that contains dates in MM-YYYY format like this − const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"]; console.log("Original array:", arr); Original array: [ '1-2016', '7-2015', '7-2016', '3-2016', '8-2016', '2-2016', '6-2016', '8-2015', '5-2016', '4-2016', '9-2015', '10-2015', '11-2015', '12-2015' ] We are required to write a JavaScript function that takes in one such array and sorts it such that the dates in the ...
Read More