Object Oriented Programming Articles

Page 182 of 589

Fetching JavaScript keys by their values - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

In JavaScript, you can find an object key by its value using several approaches. This is useful when you need to perform reverse lookups in objects where both keys and values are unique. Consider this object where we want to find keys by their corresponding values: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; console.log(products); { Pineapple: 38, Apple: 110, Pear: 109 } Using Object.keys() with find() The most straightforward approach uses Object.keys() with find() to locate the ...

Read More

Group values on same property - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 376 Views

When working with arrays of objects, you often need to group items that share the same property value. This tutorial shows how to group objects by a common property and combine other properties. The Problem Suppose we have an array of objects with unit and brand properties: const arr = [ {unit: 35, brand: 'CENTURY'}, {unit: 35, brand: 'BADGER'}, {unit: 25, brand: 'CENTURY'}, {unit: 15, brand: 'CENTURY'}, {unit: 25, brand: 'XEGAR'} ]; console.log("Original array:"); console.log(arr); ...

Read More

Sum arrays repeated value - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

Suppose, we have an array of objects like this − const arr = [ {'ID-01':1}, {'ID-02':3}, {'ID-01':3}, {'ID-02':5} ]; We are required to add the values for all these objects together that have identical keys Therefore, for this array, the output should be − const output = [{'ID-01':4}, {'ID-02':8}]; We will loop over the array, check for existing objects with the same keys, if they are there, we add value to it otherwise we push new objects to the ...

Read More

Convert JS array into an object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 401 Views

Converting a JavaScript array into an object is a common task in web development. This article shows different approaches to transform an array of objects into a single object using various JavaScript methods. Suppose we have an array of objects like this: const arr = [ {id: 1, name: "Mohan"}, {id: 2, name: "Sohan"}, {id: 3, name: "Rohan"} ]; console.log("Input array:", arr); Input array: [ { id: 1, name: 'Mohan' }, { id: 2, name: 'Sohan' }, { id: 3, name: 'Rohan' ...

Read More

Finding the sum of unique array values - JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers. Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array. For example, if the input array is: const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; Then the output should be 25 (3 + 7 + 4 + 11 = 25), as these are the only elements that appear exactly once. Using indexOf() and lastIndexOf() ...

Read More

How to subtract elements of two arrays and store the result as a positive array in JavaScript?

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

Suppose, we have two arrays like these − const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We are required to write a JavaScript function that takes in two such arrays and returns an array of absolute difference between the corresponding elements of the array. Therefore, for these arrays, the output should look like − const output = [8, 6, 4, 1, 3, 3]; We will use a for loop and keep pushing the absolute difference iteratively into a new array and ...

Read More

Sort object array based on another array of keys - JavaScript

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

When working with arrays of objects in JavaScript, you may need to sort one array based on the order defined in another array. This is particularly useful when you have a reference array that defines the desired sorting order. Problem Statement Suppose we have two arrays like these: const arr1 = ['d', 'a', 'b', 'c']; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}]; console.log("Original arrays:"); console.log("arr1:", arr1); console.log("arr2:", arr2); Original arrays: arr1: [ 'd', 'a', 'b', 'c' ] arr2: [ { a: 1 }, { c: 3 }, { d: 4 }, { ...

Read More

How to remove blank (undefined) elements from JavaScript array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 793 Views

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 More

Sum similar numeric values within array of objects - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 954 Views

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 More

Normalize numbers in an object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 682 Views

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 More
Showing 1811–1820 of 5,881 articles
« Prev 1 180 181 182 183 184 589 Next »
Advertisements