Object Oriented Programming Articles

Page 140 of 589

How to calculate the average in JavaScript of the given properties in the array of objects

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

We have an array of objects. Each object contains a few properties and one of these properties is age: const people = [ { name: 'Anna', age: 22 }, { name: 'Tom', age: 34 }, { name: 'John', ...

Read More

JavaScript Check for case insensitive values?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

JavaScript provides several methods to check for case insensitive values. The most common approaches use toLowerCase(), toUpperCase(), or regular expressions. Using toLowerCase() Method Convert both values to lowercase before comparison: let name1 = "JOHN"; let name2 = "john"; console.log(name1.toLowerCase() === name2.toLowerCase()); // true console.log("Hello".toLowerCase() === "HELLO".toLowerCase()); // true true true Using Regular Expression Use the i flag for case insensitive matching: let allNames = ['john', 'John', 'JOHN']; let makeRegularExpression = new RegExp(allNames.join("|"), "i"); let hasValue = makeRegularExpression.test("JOHN"); console.log("Is Present=" + hasValue); // Direct regex approach let ...

Read More

How to get odd and even position characters from a string?

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

In JavaScript, you can extract characters from odd and even positions in a string using various methods. This technique is useful for string manipulation tasks like creating puzzles, encoding, or data processing. Understanding the Problem Given a string, we want to separate characters based on their position index: Even positions (0, 2, 4...): "T", "i", " ", "s", " ", "a", " ", "e", "t", "!" Odd positions (1, 3, 5...): "h", "s", "i", "", "i", "", "t", "s", "" If the string is "This is a test!" Even positions: "Ti s a ...

Read More

Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 457 Views

When working with strings that contain multiple commas and spaces, you can use regular expressions with split() and join() methods to clean them up effectively. The Problem Consider this messy string with multiple consecutive commas and spaces: var sentence = "My, , , , , , , Name, , , , is John ,, , Smith"; console.log("Original string:", sentence); Original string: My, , , , , , , Name, , , , is John ,, , Smith Solution: Using Regular Expression with split() and join() The split(/[\s, ]+/) method splits ...

Read More

Taking part from array of numbers by percent JavaScript

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

We have an array of number literals like this: const numbers = [10, 6200, 20, 20, 350, 900, 26, 78, 888, 10000, 78, 15000, 200, 1280, 2000, 450]; We need to write a function that takes an array of numbers and a percentage (0-100). The function should return the first n elements from the array that sum up to equal or just less than the specified percentage of the total array sum. Understanding the Problem Let's take a simpler example: const numbers = [12, 10, 6, 8, 4, 2, 8]; ...

Read More

How to do Butterfly Shuffle in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 450 Views

A butterfly shuffled array in JavaScript is an array of Numbers that is sorted such that the numbers decrease as we approach the center of array and increase as we approach the end of array. The biggest number is placed at the very first index. Another variation of butterfly shuffled array is where the numbers increase towards the center and decrease towards the end. In this case the smallest number is placed at the very first index. For people who come from a Mathematics background, it's somewhat relatable to the Gaussian distribution. Example Suppose we have ...

Read More

How to count a depth level of nested JavaScript objects?

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

We have an array of objects, which further have nested objects like this − const arr = [{ id: 0, children: [] }, { id: 1, children: [{ id: 2, children: [] }, { id: 3, children: [{ id: 4, children: [] }] }] }]; ...

Read More

How to split string when the value changes in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 243 Views

To split a string when the character value changes in JavaScript, you can use the match() method with a regular expression that captures consecutive identical characters. Syntax string.match(/(.)\1*/g) How the Regular Expression Works The pattern /(.)\1*/g breaks down as: (.) - Captures any single character \1 - Matches the same character as captured in group 1 * - Matches zero or more of the preceding element g - Global flag to find all matches Example var originalString = "JJJJOHHHHNNNSSSMMMIIITTTTHHH"; var regularExpression = /(.)\1*/g; console.log("The original string = ...

Read More

In JavaScript, need to perform sum of dynamic array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

Let's say, we have an array that contains the score of some players in different sports. The scores are represented like this − const scores = [ {sport: 'cricket', aman: 54, vishal: 65, jay: 43, hardik: 88, karan:23}, {sport: 'soccer', aman: 14, vishal: 75, jay: 41, hardik: 13, karan:73}, {sport: 'hockey', aman: 43, vishal: 35, jay: 53, hardik: 43, karan:29}, {sport: 'volleyball', aman: 76, vishal: 22, jay: 36, hardik: 24, karan:47}, {sport: 'baseball', aman: 87, vishal: 57, jay: 48, hardik: 69, karan:37}, ]; We need to ...

Read More

Update JavaScript object with another object, but only existing keys?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 983 Views

To update a JavaScript object with values from another object while only affecting existing keys, you can use hasOwnProperty() to check if a key exists in the source object before updating it. Example var markDetails1 = { 'marks1': 78, 'marks2': 65 }; var markDetails2 = { 'marks2': 89, 'marks3': 90 }; function updateJavaScriptObject(details1, details2) { const outputObject = {}; Object.keys(details1) .forEach(obj => outputObject[obj] ...

Read More
Showing 1391–1400 of 5,881 articles
« Prev 1 138 139 140 141 142 589 Next »
Advertisements