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 460 of 801
Sorting objects according to days name JavaScript
Let's say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday. Sample Data Following is our array: const weather = [{ day: 'Wednesday', humidity: 60 }, { day: 'Saturday', humidity: 50 }, { ...
Read MoreRecursive multiplication in array - JavaScript
We need to write a JavaScript function that takes a nested array containing numbers, false values, zeros, and strings, then returns the product of all valid numbers. The function should ignore zeros and falsy values while recursively processing nested arrays. Problem Statement Given a nested array with mixed data types, we want to multiply only the truthy numeric values while ignoring zeros, null, undefined, false, and strings. Solution We'll use recursion to traverse nested arrays and multiply only valid numbers: const arr = [1, 5, 2, null, [ 2, 5, ...
Read MoreRemove number properties from an object JavaScript
In JavaScript, we often need to filter object properties based on their data types. This article demonstrates how to remove properties of a specific type from an object using a reusable function. Problem Statement Given an object with mixed property types (numbers, strings, booleans, objects), we need to create a function that removes all properties of a specified data type. If no type is specified, it should default to removing number properties. Solution We'll create a function called shedData that iterates through object properties and deletes those matching the specified type: const obj = ...
Read MoreFind Second most frequent character in array - JavaScript
We are required to write a JavaScript function that takes in an array and returns the element that appears for the second most number of times. Let's say the following is our array: const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; The most frequent element is 6 (appears 4 times), but we want the second most frequent element, which is 4 (appears 3 times). Example const arr = [1, 34, 4, 3, 2, 1, 4, 6, 4, 6, 5, 3, 6, 6]; ...
Read MoreCheck if object contains all keys in JavaScript array
We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array. So, let's write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that's not a key of object, we exit and return false, otherwise we return true. Example const obj = { ...
Read MoreDivide a string into n equal parts - JavaScript
We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string). We need to return an array of string of length n containing n equal parts of the string. Let's write the code for this function − Example const str = 'this is a sample string'; const num = 5; const divideEqual = (str, num) => { const len = str.length / num; const creds = str.split("").reduce((acc, val) => { ...
Read MoreAlgorithm to dynamically populate JavaScript array with zeros before and after values
We are given a months array, which contains elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements. If the element is present in the original array we use that element, otherwise we use 0 at that place. For example: Input → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 0, 0, 0] Now, let's write the code: Using Array.includes() Method const months = [6, ...
Read MoreFind the average of all elements of array except the largest and smallest - JavaScript
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 MoreFind all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)
We are given an array of numbers and a target sum. Our job is to write a function that returns an array of all the subarrays which add up to the target number using the sliding window algorithm. For example: const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, 27]; const sum = 40; Should find these subarrays that sum to 40: [ [ 5, 1, 34 ], [ 9, 31 ], [ 6, 7, 27 ] ] The Sliding Window Algorithm The sliding window algorithm ...
Read MoreSort array based on presence of fields in objects JavaScript
In JavaScript, you can sort arrays of objects based on the presence of specific fields using a custom comparator function. This technique is useful when you need to prioritize objects with complete information. Problem Statement Let's say we have an array of people objects where some have both firstName and lastName, some have only one of these properties, and others have neither: const people = [{ firstName: 'Ram', id: 301 }, { firstName: 'Shyam', lastName: 'Singh', ...
Read More