Object Oriented Programming Articles

Page 31 of 589

Constructing a nested JSON object in JavaScript

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

In JavaScript, we often need to construct nested objects from structured data. This article demonstrates how to build a nested JSON object from a string containing paired characters. Problem Statement Given a string with characters in pairs, we need to construct a nested object where each pair becomes a code property, and each level has a sub-object for the next pair. Input string: const str = "AABBCCDDEE"; Expected output structure: const obj = { code: "AA", sub: { ...

Read More

Grouping array of array on the basis of elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

When working with arrays of arrays in JavaScript, you might need to group elements based on specific criteria. This article demonstrates how to group the second elements of subarrays that share the same first element. Problem Statement Suppose we have an array of arrays where each subarray contains exactly two elements: const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]]; console.log("Input array:", arr); Input array: [ [ 1, 45 ], [ 1, 34 ], [ 1, 49 ], [ 2, 34 ], [ 4, 78 ...

Read More

Reverse mapping an object in JavaScript

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

Reverse mapping an object in JavaScript involves swapping the keys and values of an object. This technique is useful when you need to look up keys based on their values. Suppose we have an object like this: const products = { "Pineapple": 38, "Apple": 110, "Pear": 109 }; All the keys are unique in themselves and all the values are unique in themselves. We need to write a function that accepts a value and returns its corresponding key. Method 1: Using Object.keys() with map() ...

Read More

Add matching object values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

In JavaScript, you can add matching object values by iterating through an array of objects and accumulating values for common keys. This is useful for aggregating data from multiple objects. Consider an array of objects like this: const arr = [{a: 2, b: 5, c: 6}, {a: 3, b: 4, d: 1}, {a: 1, d: 2}]; console.log("Input array:", arr); Input array: [ { a: 2, b: 5, c: 6 }, { a: 3, b: 4, d: 1 }, { a: 1, d: 2 } ] Each object has unique properties within itself, ...

Read More

Summing up unique array values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 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. Problem Understanding If the input array is: const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; The unique elements (appearing only once) are: 3, 7, 4, 11. Their sum is 3 + 7 + 4 + 11 = 25. Using indexOf() and lastIndexOf() We ...

Read More

Absolute difference of Number arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

In JavaScript, calculating the absolute difference between corresponding elements of two arrays is a common operation. This involves subtracting elements at the same index positions and taking the absolute value of the result. Suppose we have two arrays like these: const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3]; We need to create a function that returns an array of absolute differences between corresponding elements. For these arrays, the expected output should be: [8, 6, 4, 1, 3, 3] Using For Loop ...

Read More

Sort the second array according to the elements of the first array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

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("First array:", arr1); console.log("Second array:", arr2); First array: [ 'd', 'a', 'b', 'c' ] Second array: [ { a: 1 }, { c: 3 }, { d: 4 }, { b: 2 } ] We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array. We have to sort the keys of ...

Read More

Pushing false objects to bottom in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 223 Views

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, ...

Read More

Non-composite numbers sum in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 Views

In JavaScript, you can calculate the sum of all prime numbers in an array by first checking each number for primality, then adding the prime numbers together. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Understanding Prime Numbers Prime numbers are numbers like 2, 3, 5, 7, 11, 13, etc. The number 1 is not considered prime, and 2 is the only even prime number. Creating a Prime Check Function First, we need a helper function to determine if a number is prime: ...

Read More

Summing up digits and finding nearest prime in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 154 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum. This problem involves two main steps: calculating the digit sum and finding the nearest prime number greater than or equal to that sum. Breaking Down the Solution Our solution requires three functions: digitSum() - calculates the sum of all digits in a number isPrime() - checks if a number is prime nearestPrime() - finds the nearest prime >= digit sum ...

Read More
Showing 301–310 of 5,881 articles
« Prev 1 29 30 31 32 33 589 Next »
Advertisements