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
Object Oriented Programming Articles
Page 20 of 589
What is the best way to reduce and merge a collection of objects – JavaScript?
The best way to reduce and merge a collection of objects is to use Object.values() combined with reduce() to group objects by a key and merge their properties. This approach is useful when you have duplicate objects and want to consolidate them, such as combining student records with the same ID. Example Data Consider this collection of student objects with duplicates: var details = [ { studentId: 10, marks: 75, studentName: "John" }, { studentId: 10, marks: 75, studentName: "John" }, { studentId: ...
Read MoreDetermining isomorphic strings JavaScript
Two strings are isomorphic if characters in one string can be mapped to characters in another string while preserving the structure. Each character must map to exactly one character, and no two characters can map to the same character. Understanding Isomorphic Strings For strings to be isomorphic: They must have the same length Characters at the same positions must follow a consistent mapping pattern The mapping must be bijective (one-to-one) Example const str1 = 'egg'; const str2 = 'add'; // Check if strings are isomorphic const isIsomorphic = (str1 = '', str2 ...
Read MoreHow to convert a string with zeros to number in JavaScript?
When you have a string containing numbers with dots or zeros as separators, you can convert it to a number using JavaScript's built-in methods. This is commonly needed when processing formatted numeric data. The Problem Consider a string like "453.000.00.00.0000" where dots are used as thousand separators rather than decimal points. Direct conversion methods like Number() won't work correctly because JavaScript interprets the first dot as a decimal separator. let stringValue = "453.000.00.00.0000"; console.log("Original string:", stringValue); console.log("Direct Number() conversion:", Number(stringValue)); // NaN Original string: 453.000.00.00.0000 Direct Number() conversion: NaN Using parseInt() ...
Read MoreRemoving duplicates and keep one instance in JavaScript
In JavaScript, you often need to remove duplicate values from an array while keeping only one instance of each value. There are several effective methods to accomplish this task. Method 1: Using Set (Recommended) The most modern and efficient approach uses the Set object, which automatically removes duplicates: const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; // Using Set to remove duplicates const uniqueArr = [...new Set(arr)]; console.log(uniqueArr); [ 1, 5, 7, 4, 6, 8 ] Method 2: Using filter with indexOf ...
Read MoreHow to make filter and word replacement method - JavaScript?
In JavaScript, there's no built-in method to replace all occurrences of a word in a string. You can create custom functions using methods like split() and join(), or use modern approaches like replaceAll(). Let's explore different methods to filter and replace words in a string: var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; Method 1: Using split() and join() This method splits the string by the target word and joins the parts with the replacement: var sentence = "Yes, My Name ...
Read MoreSort an array and place a particular element as default value in JavaScript
We are required to write a JavaScript function that takes in an array of literal values as the first argument and a string as the second argument. Our function should sort the array alphabetically but keeping the string provided as the second argument (if it exists in the array) as the first element, irrespective of the text it contains. Example The code for this will be − const arr = ["Apple", "Orange", "Grapes", "Pineapple", "None", "Dates"]; const sortKeepingConstants = (arr = [], text = '') => { const sorter = (a, ...
Read MoreHow to get only first word of object's value – JavaScript?
When working with objects that contain string values with multiple words, you often need to extract only the first word. The most effective approach is using the split() method with a space delimiter. Sample Data Let's work with an employee object containing names and technologies: const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java ...
Read MoreGrouping nested array in JavaScript
Suppose, we have an array of values like this − const arr = [ { value1:[1, 2], value2:[{type:'A'}, {type:'B'}] }, { value1:[3, 5], value2:[{type:'B'}, {type:'B'}] } ]; We are required to write a JavaScript function that takes in one such array. Our function should then prepare an array ...
Read MoreGet max value per key in a JavaScript array
When working with arrays of objects in JavaScript, you might need to find the object with the maximum value for a specific property within each group. This is a common task when analyzing data grouped by categories. Consider this array of fruit data: const arr = [ {a:1, b:"apples"}, {a:3, b:"apples"}, {a:4, b:"apples"}, {a:1, b:"bananas"}, {a:3, b:"bananas"}, {a:5, b:"bananas"}, {a:6, b:"bananas"}, {a:3, b:"oranges"}, ...
Read MoreGet the max n values from an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument. Our function should then pick the n greatest numbers from the array and return a new array consisting of those numbers. Using Sort and Slice The most straightforward approach is to sort the array in descending order and take the first n elements: const arr = [3, 4, 12, 1, 0, 5, 22, 20, 18, 30, 52]; const pickGreatest = (arr = [], num = 1) ...
Read More