Object Oriented Programming Articles

Page 134 of 589

Remove duplicates from a array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 725 Views

Removing duplicates from an array of objects is a common task in JavaScript. We need to identify objects with the same properties and values, keeping only unique objects in the result. Using JSON.stringify() with Map The most straightforward approach uses JSON.stringify() to convert objects to strings for comparison: const arr = [ { "timestamp": 564328370007, "message": "It will rain today" }, { ...

Read More

Conditionally change object property with JavaScript?

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

To conditionally change object properties in JavaScript, you can use the logical AND operator (&&) combined with the spread operator. This approach allows you to merge properties into an object only when a condition is true. How It Works The logical AND operator returns the second operand if the first is truthy, or false if the first is falsy. When spreading false into an object, it has no effect, making it perfect for conditional property assignment. Syntax let newObject = { ...originalObject, ...condition && { propertyName: value ...

Read More

If string includes words in array, remove them JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 853 Views

In JavaScript, you may need to remove specific words from a string based on an array of target words. This is useful for content filtering, text cleaning, or removing unwanted terms from user input. Problem Overview We need to create a function that removes all occurrences of words present in an array from a given string, while handling whitespace properly to avoid multiple consecutive spaces. Method 1: Using reduce() with Regular Expressions const string = "The weather in Delhi today is very similar to the weather in Mumbai"; const words = [ ...

Read More

Accessing variables in a constructor function using a prototype method with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 998 Views

In JavaScript constructor functions, you can access instance variables from prototype methods using the this keyword. Prototype methods share behavior across all instances while maintaining access to individual instance data. How Constructor Functions Work When you create a constructor function, instance variables are defined using this.propertyName. Prototype methods can then access these variables through this. Example function Customer(fullName){ this.fullName = fullName; } Customer.prototype.setFullName = function(newFullName){ this.fullName = newFullName; } Customer.prototype.getFullName = function(){ return this.fullName; } var customer = new Customer("John ...

Read More

Compress array to group consecutive elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 446 Views

We are given a string that contains some repeating words separated by dash (-) like this: const str = 'monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday'; console.log(str); monday-sunday-tuesday-tuesday-sunday-sunday-monday-monday-monday Our job is to write a function that returns an array of objects, where each object contains two properties: val (the word) and count (their consecutive appearance count). Expected Output Format For the above string, the compressed array should look like this: const arr = [{ val: 'monday', count: 1 }, { val: 'sunday', ...

Read More

How could I write a for loop that adds up all the numbers in the array to a variable in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 994 Views

To sum all numbers in an array, initialize a total variable to 0, then iterate through the array and add each element to the total. Let's say the following is our array with numbers: var listOfValues = [10, 3, 4, 90, 34, 56, 23, 100, 200]; Using a for Loop var listOfValues = [10, 3, 4, 90, 34, 56, 23, 100, 200]; var total = 0; for (let index = 0; index < listOfValues.length; index++) { total = total + listOfValues[index]; } console.log("Total Values = " + ...

Read More

How to calculate total time between a list of entries?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 230 Views

Let's say, we have an array that contains some data about the speed of a motor boat during upstreams and downstreams like this − Following is our sample array − const arr = [{ direction: 'upstream', velocity: 45 }, { direction: 'downstream', velocity: 15 }, { direction: 'downstream', velocity: 50 }, { direction: 'upstream', velocity: 35 }, { direction: 'downstream', velocity: 25 }, { ...

Read More

Display all the numbers from a range of start and end value in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 940 Views

In JavaScript, you can display all numbers within a specified range using various methods. The most common approach is using a for loop to iterate through the range. Using for Loop Here's how to display numbers from a start value to an end value using a for loop: var startValue = 10; var endValue = 20; var result = []; function printAllValues(start, end) { for (var i = start; i < end; i++) { result.push(i); } } printAllValues(startValue, ...

Read More

How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 414 Views

We need to write a function that checks if the characters from one string can be rearranged to form another string. This is essentially checking if one string contains all the characters needed to build the second string. The function scramble(str1, str2) should return true if characters from str1 can be rearranged to match str2, otherwise false. Problem Examples str1 = 'cashwool', str2 = 'school' → true (cashwool contains: c, a, s, h, w, o, o, l - enough to make 'school') str1 = 'katas', str2 = 'steak' → false (katas missing 'e' ...

Read More

How to modify key values in an object with JavaScript and remove the underscore?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 618 Views

In JavaScript, you can modify object keys to remove underscores and convert them to camelCase using regular expressions combined with Object.fromEntries() and Object.entries(). Syntax const camelCaseKey = str => str.replace(/(_)(.)/g, (_, __, char) => char.toUpperCase()); const newObject = Object.fromEntries( Object.entries(originalObject).map(([key, value]) => [camelCaseKey(key), value]) ); Example // Function to convert underscore keys to camelCase var underscoreSpecifyFormat = str => str.replace(/(_)(.)/g, (_, __, v) => v.toUpperCase()); // Original object with underscore keys var JsonObject = { first_Name_Field: 'John', last_Name_Field: ...

Read More
Showing 1331–1340 of 5,881 articles
« Prev 1 132 133 134 135 136 589 Next »
Advertisements