Object Oriented Programming Articles

Page 117 of 589

Sum of nested object values in Array using JavaScript

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

When working with complex nested data structures in JavaScript, you often need to sum values from deeply nested objects within arrays. This guide demonstrates various approaches to calculate the sum of nested object values efficiently. Understanding the Data Structure Consider a JSON object with nested arrays and objects where we need to sum the costNum values: let json = { storeData: [ { items: [ ...

Read More

De-structuring an object without one key

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

Object destructuring with the rest operator (...) allows you to extract specific properties while collecting the remaining properties into a new object. This technique is useful when you want to exclude certain keys from an object. Syntax const { keyToExclude, ...remainingKeys } = originalObject; Example: Excluding One Key Object Destructuring body { ...

Read More

Group array by equal values JavaScript

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

Let's say, we have an array of string/number literals that contains some duplicate values like this: const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night']; We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are grouped together in a subarray as the first element and their total count in the original array as the second element. So, for this example, the output should be: [ [ 'day', 3 ], ...

Read More

Can we modify built-in object prototypes in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 216 Views

Yes, JavaScript allows modifying built-in object prototypes, but it should be done carefully. You can extend native objects like String, Array, or global functions like alert(). What are Built-in Object Prototypes? Built-in object prototypes are the foundation objects that JavaScript provides, such as String.prototype, Array.prototype, and global functions like alert(). Modifying them affects all instances of that type. Example: Overriding the alert() Function Custom Alert Function ...

Read More

Binding an object's method to a click handler in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 288 Views

Binding an object's method to a click handler is essential when you need to maintain the correct this context within the method. This ensures the method can access the object's properties and other methods properly. Understanding the Context Problem When directly assigning an object method to an event handler, the this context gets lost and refers to the event target instead of the original object. Example Binding Object Methods to Click Handlers ...

Read More

Can we share a method between JavaScript objects in an array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

Yes, we can share methods between JavaScript objects in an array using prototypes or by defining shared methods outside the objects. This approach promotes code reusability and memory efficiency. Using Prototype Methods The most common way to share methods is through prototype inheritance. When you define a method on a constructor's prototype, all instances share that method. Shared Methods in JavaScript body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { ...

Read More

Form Object from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 311 Views

We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0. For example − // if the input string is: const str = 'hello world!'; // then the output should be: const obj = {"h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0}; So, let's write the code for this function − ...

Read More

Referring JavaScript function from within itself

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 Views

In JavaScript, a function can call itself from within its own body, which is known as recursion. This technique is useful for tasks like calculating factorials, traversing nested structures, or repeating operations until a condition is met. What is Recursion? Recursion occurs when a function calls itself. Every recursive function needs two key elements: a base case (condition to stop) and a recursive case (function calling itself with modified parameters). Basic Example: Factorial Function Recursion Example ...

Read More

Check for Power of two in JavaScript

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

We need to write a function, isPowerOfTwo(), that takes a positive number and returns a boolean based on whether the number is a power of 2. For example: isPowerOfTwo(3) // false (3 is not a power of 2) isPowerOfTwo(32) // true (32 = 2^5) isPowerOfTwo(2048) // true (2048 = 2^11) isPowerOfTwo(256) // true (256 = 2^8) isPowerOfTwo(22) // false (22 is not a power of 2) Method 1: Using Recursive Division This approach recursively divides the number by 2 until it reaches 1 (power of 2) ...

Read More

Parsing array of objects inside an object using maps or forEach using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 840 Views

When working with nested data structures in JavaScript, you often need to parse arrays of objects within objects. This can be efficiently done using map() or forEach() methods to iterate through the data and extract the information you need. Understanding the Data Structure Consider a JSON object containing store data with nested arrays of items: let json = { storeData: [ { items: [ ...

Read More
Showing 1161–1170 of 5,881 articles
« Prev 1 115 116 117 118 119 589 Next »
Advertisements