Javascript Articles

Page 102 of 534

How to filter out common array in array of arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 303 Views

When working with arrays of arrays in JavaScript, you may need to remove duplicate subarrays and keep only unique ones. This is useful for data deduplication and filtering operations. The Problem Suppose we have an array of arrays with duplicate subarrays: const arr = [ [ "Serta", "Black Friday" ], [ "Serta", ...

Read More

Combine two different arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

Suppose we have two arrays, the first array contains the scheduled date for some events and the second array contains the names of those events, like this − const dates = [ { id: "1", date: "2017-11-07" }, { id: "1", date: "2017-11-08" }, { ...

Read More

Center of each side of a polygon in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 239 Views

In JavaScript, finding the center (midpoint) of each side of a polygon involves calculating the average coordinates between consecutive vertices. This is useful in computational geometry for polygon analysis and graphics applications. Problem Definition Given an array of coordinate pairs representing polygon vertices, we need to find the midpoint of each side. Each side connects two consecutive vertices, and the last vertex connects back to the first vertex to close the polygon. // Example polygon coordinates (longitude, latitude) const polygonVertices = [ [-73.9280684530257, 40.8099975343718], [-73.9282820374729, 40.8100875554645], ...

Read More

Iterate over an object and remove false property in JavaScript

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

In JavaScript, you may need to recursively iterate through an object and remove properties with falsy values. This is common when cleaning up configuration objects or filtering data structures. The Problem Consider an object with nested structures containing "enabled" properties. We want to remove any objects where enabled is false, and also clean up any empty parent objects left behind. const obj = { a: { someKey: { propOne: '', ...

Read More

Grouping array values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 519 Views

Suppose we have an array of objects containing some years and weeks data like this: const arr = [ {year: 2017, week: 45}, {year: 2017, week: 46}, {year: 2017, week: 47}, {year: 2017, week: 48}, {year: 2017, week: 50}, {year: 2017, week: 52}, {year: 2018, week: 1}, {year: 2018, week: 2}, {year: 2018, week: 5} ]; We are required to ...

Read More

Pushing positives and negatives to separate arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 439 Views

We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array. We will be using the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays. Using Array.reduce() Method The code for this will be − const arr = [97, -108, 13, -12, 133, -887, 32, -15, 33, -77]; const splitArray = (arr) => { return arr.reduce((acc, val) => { ...

Read More

Sum all duplicate values in array in JavaScript

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

We need to write a JavaScript function that takes an array of numbers with duplicate entries and sums all duplicate values. Each unique number appears once in the result, multiplied by its frequency count. Problem Understanding For array [1, 3, 1, 3, 5, 7, 5, 3, 4]: 1 appears 2 times → 1 × 2 = 2 3 appears 3 times → 3 × 3 = 9 5 appears 2 times → 5 × 2 = 10 7 appears 1 time → 7 × 1 = 7 4 appears 1 time → 4 × 1 = 4 ...

Read More

Finding deviations in two Number arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

We are required to write a JavaScript function that takes in two number arrays and returns the elements that are not common to both arrays (symmetric difference). For example, if the two arrays are: const arr1 = [2, 4, 2, 4, 6, 4, 3]; const arr2 = [4, 2, 5, 12, 4, 1, 3, 34]; Then the output should be: [6, 5, 12, 1, 34] Method 1: Using indexOf() Method This approach uses nested loops to check if elements exist in the other array: const arr1 = [2, ...

Read More

Excluding extreme elements from average calculation in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 262 Views

We need to write a JavaScript function that calculates the average of array elements while excluding the smallest and largest values. This is useful when you want to remove outliers from statistical calculations. Problem Statement Given an array of numbers, calculate the average excluding the minimum and maximum values. This helps eliminate extreme values that might skew the results. Example Let's implement a function that finds the excluded average: const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => { const creds ...

Read More

Check for perfect square in JavaScript

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

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. For example, 16 is a perfect square because 4 × 4 = 16. Examples of Perfect Square Numbers 144 (12²), 196 (14²), 121 (11²), 81 (9²), 484 (22²) Method 1: Using Math.sqrt() The most straightforward approach is to find the square root and check if ...

Read More
Showing 1011–1020 of 5,340 articles
« Prev 1 100 101 102 103 104 534 Next »
Advertisements