AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 475 of 840

Group values in array by two properties JavaScript

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

We have an array of objects like this: const arr = [ { value: 12, gap: 1 }, { value: 13, gap: 1 }, { value: 14, gap: 1 }, { value: 15, gap: 1 }, { value: 19, gap: 2 }, { value: 21, gap: 1 }, { value: 22, gap: 1 }, { value: 23, gap: 1 }, { value: ...

Read More

Program to test the equality of two arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 191 Views

We are required to write a JavaScript function that takes in two arrays of literals and checks the corresponding elements of the array and it should return true if all the corresponding elements of the array are equal otherwise it should return false. Let's write the code for this function − Example Following is the code − const arr1 = [1, 4, 5, 3, 5, 6]; const arr2 = [1, 4, 5, 2, 5, 6]; const areEqual = (first, second) => { if(first.length !== second.length){ ...

Read More

How to compare two string arrays, case insensitive and independent about ordering JavaScript, ES6

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 856 Views

We need to write a function that compares two strings to check if they contain the same characters, ignoring case and order. This is useful for validating anagrams or checking string equivalence. For example: const first = 'Aavsg'; const second = 'VSAAg'; isEqual(first, second); // true Using Array Sort Method This method converts strings to arrays, sorts the characters, and compares the results. We extend the String prototype to add a sort method. const first = 'Aavsg'; const second = 'VSAAg'; const stringSort = function(){ return this.split("").sort().join(""); ...

Read More

Check whether a value exists in JSON object?

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

In JavaScript, you can check whether a value exists in a JSON object (or array of objects) using several approaches. Let's explore different methods to find values efficiently. Consider the following JSON object structure: var apiJSONObject = [ {subjectName: "MySQL"}, {subjectName: "Java"}, {subjectName: "JavaScript"}, {subjectName: "MongoDB"} ] Using for Loop (Traditional Approach) The basic approach uses a for loop to iterate through the array and check each object: var apiJSONObject = [ {subjectName: "MySQL"}, ...

Read More

Find the average of all elements of array except the largest and smallest - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the average of its elements excluding the smallest and largest numbers. Approach The solution involves finding the minimum and maximum values, calculating the total sum, then computing the average of remaining elements after excluding min and max values. Example const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const findExcludedAverage = arr => { const creds = arr.reduce((acc, val) => { ...

Read More

JavaScript tracking the differences between elements in an array

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

Tracking differences between elements in an array is a fundamental task in data analysis and software development. Whether you're analyzing trends in numerical data, measuring changes over time, or debugging an application's behavior, calculating these differences can provide valuable insights. In JavaScript, this task is often approached using various techniques, including loops and modern functional programming methods like the map() function. Here are two methods to calculate differences between elements in an array: Iterative Loop and Functional Programming using the map. Problem Statement We are given an array of Number literals, and we are required to write ...

Read More

Reduce an array to the sum of every nth element - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array. Let's write the code for this function − Example const arr = [1, 4, 5, 3, 5, 6, 12, 5, 65, 3, 2, 65, 9]; const num = 2; const nthSum = (arr, num) => { let sum = 0; for(let i = 0; i < arr.length; i++){ ...

Read More

How to disable mouse event on certain elements using JavaScript?

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

In JavaScript, you can disable mouse events on specific elements using the CSS pointer-events property. This technique prevents elements from receiving mouse clicks, hover effects, and other pointer interactions. Using pointer-events CSS Property The most effective method is setting pointer-events: none via JavaScript. This disables all mouse interactions on the target element. Disable Mouse Events body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; ...

Read More

Find Max Slice Of Array | JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 279 Views

In JavaScript, finding the maximum slice of an array with at most two different numbers is a common problem that can be efficiently solved using the sliding window technique. This approach maintains a dynamic window that expands and contracts to find the longest contiguous subarray containing no more than two distinct elements. How It Works The sliding window algorithm uses two pointers (start and end) to define a window boundary. We track distinct elements using a map and adjust the window size when the distinct count exceeds two. Example const arr = [1, 1, 1, ...

Read More

How to get key name when the value contains empty in an object with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 891 Views

To find keys with empty values in a JavaScript object, you can use Object.keys() combined with find() or filter(). This is useful for form validation or data cleaning. Let's say we have the following object: var details = { firstName: 'John', lastName: '', countryName: 'US' }; Using Object.keys() with find() To find the first key with an empty value, use Object.keys() along with find(): var details = { firstName: 'John', lastName: '', countryName: 'US' ...

Read More
Showing 4741–4750 of 8,392 articles
« Prev 1 473 474 475 476 477 840 Next »
Advertisements