AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 390 of 840

How to check if an array contains integer values in JavaScript ?

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

In JavaScript, checking if an array contains integer values requires understanding the difference between numbers and strings that look like numbers. This article explores different approaches to detect actual integer values in arrays. The Problem with String Numbers Arrays often contain string representations of numbers like "123" instead of actual numbers. We need to distinguish between these types. const mixedArray = ["123", 45, "hello", 67.5, "89"]; console.log(typeof "123"); // "string" console.log(typeof 45); // "number" string number Method 1: Using typeof and Number.isInteger() The most ...

Read More

Finding all peaks and their positions in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 684 Views

A peak (local maximum) in an array is an element that is greater than both its neighbors. Finding all peaks and their positions is useful for data analysis, signal processing, and identifying trends. Problem Statement Given an array of integers, we need to find all local maxima (peaks) and return an object containing two arrays: maximas (the peak values) and positions (their indices). Consider this array: const arr = [4, 3, 4, 7, 5, 2, 3, 4, 3, 2, 3, 4]; console.log("Array:", arr); Array: [4, 3, 4, 7, 5, 2, 3, 4, ...

Read More

Program to implement Bucket Sort in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 741 Views

Bucket Sort is an efficient sorting algorithm that works by distributing elements into multiple buckets based on their values, then sorting each bucket individually. This approach is particularly effective when the input is uniformly distributed across a range. How Bucket Sort Works The algorithm follows these key steps: Find the minimum and maximum values in the array Create a specific number of buckets to hold ranges of values Distribute array elements into appropriate buckets Sort each bucket using a suitable sorting algorithm (like insertion sort) Concatenate all sorted buckets to get the final result ...

Read More

Get the correct century from 2-digit year date value - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 385 Views

When working with 2-digit year values, you need to determine which century they belong to. A common approach is using a pivot year to decide between 19XX and 20XX centuries. Example Following is the code − const yearRangeValue = 18; const getCorrectCentury = dateValues => { var [date, month, year] = dateValues.split("-"); var originalYear = +year > yearRangeValue ? "19" + year : "20" + year; return new Date(originalYear + "-" + month + "-" + date).toLocaleDateString('en-GB') }; console.log(getCorrectCentury('10-JAN-19')); console.log(getCorrectCentury('10-JAN-17')); console.log(getCorrectCentury('10-JAN-25')); ...

Read More

How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 615 Views

When working with flattened objects that use dot notation (like "car.make" or "visits.0.date"), you often need to convert them back into nested objects and arrays. This process is called "unflattening" and is useful when working with form data, APIs, or database results. The Problem Suppose we have a flattened object like this: const obj = { "firstName": "John", "lastName": "Green", "car.make": "Honda", "car.model": "Civic", "car.revisions.0.miles": 10150, "car.revisions.0.code": "REV01", ...

Read More

How to convert nested array pairs to objects in an array in JavaScript ?

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

When working with complex data structures, you often need to convert nested array pairs into objects. This is common when processing form data or API responses that use key-value pair arrays. Problem Statement Suppose we have an array of arrays like this: const arr = [ [ ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'] ], ...

Read More

Removing comments from array of string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 423 Views

We are required to write a JavaScript function that takes in array of strings, arr, as the first argument and an array of special characters, starters, as the second argument. The starter array contains characters that can start a comment. Our function should iterate through the array arr and remove all the comments contained in the strings. Problem Example For example, if the input to the function is: const arr = [ 'red, green !blue', 'jasmine, #pink, cyan' ]; const starters = ['!', '#']; Then the output ...

Read More

How to merge specific elements inside an array together - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

When working with arrays containing mixed data types, you might need to merge consecutive numeric elements while keeping certain separators intact. This is common when processing data that represents grouped numbers. Let's say we have the following array: var values = [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8]; console.log("Original array:", values); Original array: [7, 5, 3, 8, 9, '/', 9, 5, 8, 2, '/', 3, 4, 8] Using join() and split() Method To merge specific elements while preserving separators, we can use join(), split(), ...

Read More

Get all methods of any object JavaScript

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

In JavaScript, you can extract all methods (functions) from any object using Object.getOwnPropertyNames() combined with type filtering. This is useful for debugging, reflection, or dynamically working with object methods. Understanding the Problem Objects contain both properties (data) and methods (functions). To get only the methods, we need to: Get all property names from the object Filter properties where the value type is "function" Return an array of method names Using Object.getOwnPropertyNames() The Object.getOwnPropertyNames() method returns an array of all properties (enumerable and non-enumerable) found directly on the given object. We then filter this ...

Read More

Sort an array of objects by multiple properties in JavaScript

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

Sorting an array of objects by multiple properties is a common requirement in JavaScript applications. This involves creating custom comparison logic that prioritizes different properties based on specific criteria. Suppose, we have an array of objects like this: const arr = [ { id: 1, score: 1, isCut: false, dnf: false }, { id: 2, score: 2, isCut: false, dnf: false }, { id: 3, score: 3, isCut: false, dnf: false }, { id: 4, score: 4, isCut: false, dnf: false ...

Read More
Showing 3891–3900 of 8,392 articles
« Prev 1 388 389 390 391 392 840 Next »
Advertisements