Object Oriented Programming Articles

Page 164 of 589

Check whether a value exists in JSON object?

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

In JavaScript, you can check whether a value exists in a JSON object (or array of objects) using several approaches. Let's explore different methods to find values efficiently. Consider the following JSON object structure: var apiJSONObject = [ {subjectName: "MySQL"}, {subjectName: "Java"}, {subjectName: "JavaScript"}, {subjectName: "MongoDB"} ] Using for Loop (Traditional Approach) The basic approach uses a for loop to iterate through the array and check each object: var apiJSONObject = [ {subjectName: "MySQL"}, ...

Read More

Adding two values at a time from an array - JavaScript

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

Let's say, we are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array. For example, if the input array is − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; Then the output should be − const output = [9, 90, 26, 4, 14]; Example Following is the code − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; ...

Read More

How to use JavaScript map() method to access nested objects?

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

The map() method can be used to iterate through arrays of nested objects and safely access their properties using conditional checks. Understanding Nested Objects When working with arrays containing objects that have different structures, some objects may have nested properties while others don't. Here's our sample data: var details = [ { id: "101", firstName: "John", lastName: "Smith", age: 25, ...

Read More

Counting the clusters of positive numbers - JavaScript Arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

Let's say, we have an array of numbers like this − const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; We are required to write a JavaScript function that counts the consecutive groups of non-negative (positives and 0) numbers in the array. Like here we have consecutive non-negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from index 9 to end of array forms the second group. Therefore, for this array, the function should return 2. How It Works ...

Read More

How to convert comma separated text in div into separate lines with JavaScript?

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

Converting comma-separated text in a div into separate lines is a common requirement in web development. This can be achieved using JavaScript's string manipulation methods along with DOM manipulation. Let's say we have the following comma-separated text in a div: This, is, the, first, JavaScript, program To convert comma-separated text into separate lines, we need to use trim() along with split() based on the comma separator. Method 1: Converting to List Items This approach splits the comma-separated text and converts each item into a list element: ...

Read More

Code to find the center of an array without using ES6 functions - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. How It Works The solution uses recursion to count array elements by incrementing an index until reaching undefined, then calculates the middle position(s) based on whether the count is odd or even. Example ...

Read More

Call a JavaScript class object with variable?

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

In JavaScript, you can call a class constructor with variables by passing the variable as an argument. There are several ways to achieve this, including direct instantiation and using eval(). Basic Class Instantiation with Variables The most straightforward approach is to pass variables directly to the class constructor: class FirstClass { constructor(message) { this.message = message; } } var message = 'This is the Class Demo'; var object = new FirstClass(message); console.log(object.message); This is the Class ...

Read More

Counting below / par elements from an array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 248 Views

We are required to write a function that counts how many elements in an array are below or at/above a given number. Following is our array of Numbers − const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78]; For example, if the number is 60, there should be 7 elements below it − 54, 54, 43, 54, 54, 3, 23 and 5 elements at or above it − 65, 73, 78, 76, 78 Using Array.reduce() Method The reduce() method processes ...

Read More

How to group JSON data in JavaScript?

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

Grouping JSON data in JavaScript involves organizing objects by a common property. This is useful when you need to categorize data or create summary reports from complex datasets. Basic Grouping with for...in Loop The traditional approach uses a for...in loop to iterate through object keys and group items by a specific property: var details = { "1": { name: "John" }, "2": { name: "John" ...

Read More

Merging boolean array with AND operator - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 829 Views

Let's say, we have an array of arrays of boolean like this − const arr = [[true, false, false], [false, false, false], [false, false, true]]; We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator. Let's write the code for this function. We will be using Array.prototype.reduce() function to achieve this. Example Following is the code − const arr = [[true, false, false], [false, false, false], [false, false, true]]; const ...

Read More
Showing 1631–1640 of 5,881 articles
« Prev 1 162 163 164 165 166 589 Next »
Advertisements