Object Oriented Programming Articles

Page 126 of 589

How to assign values to an array with null/empty objects in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 973 Views

In JavaScript, you can assign values to an array containing null or empty objects using various methods. This is useful when you need to populate placeholder objects with actual data. Method 1: Using forEach() with Object.keys() This approach iterates through an array of empty objects and assigns properties from a source object: Assign Values to Empty Objects Assign values to an array with null/empty objects Assign Values ...

Read More

Can we convert two arrays into one JavaScript object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

In JavaScript, you can convert two arrays into a single object by using one array as keys and another as values. This is useful when you have related data stored in separate arrays. Using forEach() Method The most common approach is to use forEach() to iterate through the keys array and map each key to its corresponding value: Convert Arrays to Object Convert Two Arrays into One JavaScript Object ...

Read More

The image() object in JavaScript.

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

The Image object in JavaScript represents an HTML element and allows you to create, manipulate, and load images dynamically without adding them directly to the HTML. Creating Image Objects You can create an Image object using the new Image() constructor, optionally specifying width and height: // Basic image creation let img = new Image(); // With dimensions let imgWithSize = new Image(300, 200); Properties The Image object has several important properties: src - Sets or gets the image URL width - Sets or ...

Read More

How to merge objects into a single object array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 345 Views

In JavaScript, you can merge multiple objects from an array into a single object using various methods. This is useful when you have an array of objects with different properties and want to combine them. Using Object.assign() The Object.assign() method copies properties from source objects to a target object. Using the spread operator with it merges all objects in an array: Merge Objects body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { ...

Read More

Mersenne prime in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 356 Views

In Mathematics, a Mersenne prime is a number that can be written in the form M(n) = 2^n − 1 for some integer n and is actually a prime number. For example − The first four Mersenne primes are 3, 7, 31, and 127, which correspond to n = 2, 3, 5, and 7 respectively: 2² − 1 = 3 2³ − 1 = 7 2⁵ − 1 = 31 2⁷ − 1 = 127 We need to write a JavaScript function that ...

Read More

Is there an elegant way of passing an object of parameters into a function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 125 Views

Passing an object of parameters to a function provides flexibility and improves code readability. This technique allows you to avoid positional parameters and makes your functions more maintainable. Using Object Destructuring (Recommended) The most elegant approach uses ES6 destructuring to extract object properties directly in the function parameter: Object Parameters Example Passing Object Parameters to Functions Calculate Sum ...

Read More

Segregate all 0s on right and 1s on left in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

We have an array of Numbers that contains 0, 1 and some other numbers. We are required to write a JavaScript function that takes in this array and brings all 1s to the start and 0s to the end Let's write the code for this function − Example const arr = [3, 2, 1, 8, 9, 0, 1, 9, 0, 2, 1, 0, 2, 0, 1, 0, 1, 1, 4, 0, 3]; const segregate = arr => { const copy = arr.slice(); for(let i = 0; i ...

Read More

Is it possible to de-structure to already-declared variables? In JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

Yes, it is possible to destructure to already-declared variables in JavaScript. However, you must wrap the destructuring assignment in parentheses to avoid syntax errors. The Problem When variables are already declared, JavaScript cannot distinguish between a block statement and destructuring assignment: Destructuring Problem let name, age; ...

Read More

Wildcard matching of string JavaScript

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

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function should return true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, otherwise the function should return false. Let's write the code for this function — Example const str1 = 'first string'; const str2 = 'second string'; const wildcardMatching = (first, second, num) => { ...

Read More

How to transform JavaScript arrays using maps?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 160 Views

JavaScript's map() method creates a new array by transforming each element of the original array using a callback function. It's essential for functional programming and data transformation. Syntax array.map(callback(element, index, array)) Parameters callback - Function that transforms each element element - Current array element being processed index - Index of the current element (optional) array - The original array (optional) Basic Example: Squaring Numbers Transform Arrays with Map ...

Read More
Showing 1251–1260 of 5,881 articles
« Prev 1 124 125 126 127 128 589 Next »
Advertisements