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
Javascript Articles
Page 484 of 534
How to remove blank (undefined) elements from JavaScript array - JavaScript
When working with JavaScript arrays, you may encounter sparse arrays containing empty slots (undefined elements). These gaps can occur when elements are deleted or when arrays are created with missing values. const arr = [4, 6, , 45, 3, 345, , 56, 6]; console.log(arr); console.log("Length:", arr.length); [ 4, 6, , 45, 3, 345, , 56, 6 ] Length: 9 We need to remove only the undefined and empty values, not all falsy values like 0, false, or empty strings. Method 1: Using splice() with for loop Use a for loop to ...
Read MoreSum similar numeric values within array of objects - JavaScript
Suppose, we have an array of objects like this — const arr = [ {"firstName":"John", "value": 89}, {"firstName":"Peter", "value": 151}, {"firstName":"Anna", "value": 200}, {"firstName":"Peter", "value": 22}, {"firstName":"Anna", "value": 60} ]; We are required to write a JavaScript function that takes in one such array and combines the value property of all those objects that have similar value for the firstName property. Therefore, for the above array, the output should look like — const output = [ ...
Read MoreNormalize numbers in an object - JavaScript
Number normalization scales values from one range to another proportionally. This technique is commonly used in data processing, machine learning, and graphics programming. Problem Definition Given an object with numeric values and a target range [a, b], we need to transform all values so that: The smallest original value becomes a The largest original value becomes b Other values are scaled proportionally between a and b Normalization Formula The formula for linear normalization is: normalized_value = range_min + ((value - original_min) * (range_max - range_min)) / (original_max - original_min) ...
Read MoreFind indexes of multiple minimum value in an array in JavaScript
Suppose we have an array of numbers like this − const arr = [1, 2, 3, 4, 1, 7, 8, 9, 1]; Suppose we want to find the index of the smallest element in the array i.e. 1 above. For this, we can simply use − const min = Math.min.apply(Math, arr); const ind = arr.indexOf(min); The above code will successfully set ind to 0, which indeed is correct. But what we want to achieve is that if there are more than one minimum elements in the array, like in the ...
Read MoreReturning the highest number from object properties value – JavaScript
When working with JavaScript objects, you might need to find the property with the highest value. For example, finding the highest rating from a property rating system. Suppose we have an object that contains ratings of a property over some criteria like this: const rating = { "overall": 92, "atmosphere": 93, "cleanliness": 94, "facilities": 89, "staff": 94, "security": 92, "location": 88, "valueForMoney": 92 } We need to write a JavaScript function ...
Read MoreJavaScript - Convert an array to key value pair
Converting an array of objects to key-value pairs is a common task in JavaScript. This involves transforming structured data into a simple object where one property becomes the key and another becomes the value. Problem Statement Suppose we have an array of student objects like this: const arr = [ {"name": "Rahul", "score": 89}, {"name": "Vivek", "score": 88}, {"name": "Rakesh", "score": 75}, {"name": "Sourav", "score": 82}, {"name": "Gautam", "score": 91}, {"name": "Sunil", ...
Read MoreRemove array duplicates by property - JavaScript
When working with arrays of objects in JavaScript, you often need to remove duplicate entries based on a specific property. This tutorial shows different methods to accomplish this task. The Problem Consider an array of objects with duplicate names: const arr = [ {name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}, {name: "Jack", age: "21"} ]; console.log("Original array:"); console.log(arr); Original array: [ ...
Read MoreBuilding a Map from 2 arrays of values and keys in JavaScript
In JavaScript, you can create a Map from two separate arrays containing keys and values. This is useful when you have parallel arrays where each index corresponds to a key-value pair. Problem Setup Suppose we have two arrays: const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"]; We need to create a Map where each key from the first array maps to the corresponding value from the second array at the same index. Using a for Loop The most straightforward approach is to iterate through ...
Read MoreCounting occurrences of vowel, consonants - JavaScript
We are required to write a JavaScript function that takes in a string which contains English alphabet, for example − const str = 'This is a sample string, will be used to collect some data'; The function should return an object containing the count of vowels and consonants in the string i.e. the output should be − { vowels: 17, consonants: 29 } Understanding the Problem To count vowels and consonants, we need to: Iterate through each character in the string Check if the character ...
Read MoreSorting an array object by property having falsy value - JavaScript
Suppose, we have an array of objects like this − const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ]; We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property. Understanding Falsy Values In JavaScript, falsy values include false, null, undefined, 0, "", ...
Read More