Object Oriented Programming Articles

Page 160 of 589

How to remove li elements on button click in JavaScript?

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

In JavaScript, you can remove list items dynamically by attaching event listeners to buttons within each element. This tutorial shows how to remove specific list items when their corresponding "Remove" buttons are clicked. HTML Structure First, let's look at the basic HTML structure for our unordered list: JavaScript Remove MySQL Remove MongoDB Remove Java Remove Each list item contains a subject name and a "Remove" button. When clicked, the button will remove its parent ...

Read More

Beginning and end pairs in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

We are required to write a JavaScript function that takes in an array of Number / String literals and returns another array of arrays. With each subarray containing exactly two elements, the nth element from start and nth from last. For example, if we have an array: const arr = [1, 2, 3, 4, 5, 6]; Then the output should be: const output = [[1, 6], [2, 5], [3, 4]]; How It Works The algorithm pairs elements from both ends of the array moving inward: ...

Read More

JavaScript creating an array from JSON data?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 986 Views

To create an array from JSON data in JavaScript, you can use the map() method to extract specific values from each object. This is particularly useful when working with API responses or complex data structures. Basic JSON Data Structure Let's start with a simple JSON array containing student information: const studentDetails = [ { name: "John" }, { name: "David" }, ...

Read More

Random name generator function in JavaScript

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

We are required to write a JavaScript function that takes in a number n and returns a random string of length n containing no other than the 26 English lowercase alphabets. Example Let us write the code for this function: const num = 8; const randomNameGenerator = num => { let res = ''; for(let i = 0; i < num; i++){ const random = Math.floor(Math.random() * 26); res += String.fromCharCode(97 + ...

Read More

Filter array of objects by a specific property in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 619 Views

Filtering arrays of objects by specific properties is a common task in JavaScript. While the article shows using map() with a ternary operator for comparison, the filter() method is typically more appropriate for filtering operations. Basic Array Filtering with filter() The filter() method creates a new array with elements that pass a test condition: let customers = [ {firstName: 'John', amount: 100}, {firstName: 'David', amount: 50}, {firstName: 'Bob', amount: 80}, {firstName: 'Alice', amount: 120} ]; // Filter customers with ...

Read More

Nearest Prime to a number - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 597 Views

We are required to write a JavaScript function that takes in a number and returns the first prime number that appears after n. For example: If the number is 24, then the output should be 29. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Implementation We'll create two functions: one to check if a number is prime, and another to find the nearest prime after a given ...

Read More

Any way to solve this without concatenating these two arrays to get objects with higher value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 89 Views

To find objects with higher property values from two arrays without concatenation, use the reduce() method on both arrays individually. This approach compares objects by a specific property and keeps only those with the highest values. Problem Setup Consider two arrays containing student objects with names and marks from different sections: var sectionAStudentDetails = [ {studentName: 'John', studentMarks: 78}, {studentName: 'David', studentMarks: 65}, {studentName: 'Bob', studentMarks: 98} ]; let sectionBStudentDetails = [ {studentName: 'John', studentMarks: 67}, ...

Read More

Remove '0','undefined' and empty values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 310 Views

To remove '0', 'undefined' and empty values from an array in JavaScript, you can use several methods. The most common approaches are using splice() with a loop or the filter() method. Method 1: Using splice() with for loop This method modifies the original array by removing falsy values in place: var allValues = [10, false, 100, 150, '', undefined, 450, null, 0]; console.log("Original Array:"); console.log(allValues); for (var index = 0; index < allValues.length; index++) { if (!allValues[index]) { allValues.splice(index, 1); ...

Read More

Returning poker pair cards - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

We are required to write a function that takes in an array of exactly five elements representing the five cards of a poker player drawn randomly. If the five cards contain at least one pair, our function should return the card number of the highest pair (trivial if there only exists a single pair). Else our function should return false. For example: If the array is − const arr = ['A', 'Q', '3', 'A', 'Q']; Then our function should return − 'A' (as 'A' > 'Q' in card games) Card Ranking System ...

Read More

Un-nesting array of object in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 340 Views

To un-nest an array of objects in JavaScript, you can use the map() method to restructure nested data into a flatter format. This is useful when you have deeply nested objects and want to extract specific properties to create a simpler structure. Problem: Nested Object Structure Let's say you have an array of student objects where subject details are nested inside each student: const studentDetails = [ { "studentId": 101, "studentName": "John", ...

Read More
Showing 1591–1600 of 5,881 articles
« Prev 1 158 159 160 161 162 589 Next »
Advertisements