Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 166 of 589
Removing identical entries from an array keeping its length same - JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end. For example − If we find four duplicate values, we have to remove them all and insert four empty strings at the end. Understanding the Problem The goal is to: Remove duplicate elements from the array Keep only the last occurrence of each element Maintain the original array length by adding empty strings Example ...
Read MoreUpdating copied object also updates the parent object in JavaScript?
When copying objects in JavaScript, understanding shallow vs deep copying is crucial. Simply assigning an object creates a reference, not a true copy, which can lead to unexpected behavior where modifying the "copy" also updates the original. The Problem with Direct Assignment Direct assignment creates a reference to the same object in memory: var original = { name: 'John', age: 30 }; var copy = original; // This creates a reference, not a copy copy.name = 'Smith'; console.log("Original:", original); console.log("Copy:", copy); Original: { name: 'Smith', age: 30 } Copy: { name: ...
Read MoreRemove elements from array using JavaScript filter - JavaScript
Suppose, we have two arrays of literals like these − const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array. And then return the filtered array to get the below output − const output = [7, 6, 3, 6, 3]; Method 1: Using filter() with indexOf() The filter() method creates ...
Read MoreHow do I display the content of a JavaScript object in a string format?
To display JavaScript object content as a string, you have several methods. The most common approach is using JSON.stringify() which converts objects into readable JSON format. Using JSON.stringify() The JSON.stringify() method converts JavaScript objects into JSON strings: Display JavaScript Object // Create a JavaScript object var ...
Read MoreCompare array elements to equality - JavaScript
In JavaScript, you can compare array elements at corresponding positions to count how many values match. This is useful for analyzing similarities between two arrays in a sequence-dependent manner. For example, if you have two arrays: const arr1 = [4, 7, 4, 3, 3, 3, 7, 6, 5]; const arr2 = [6, 5, 4, 5, 3, 2, 5, 7, 5]; The function should compare arr1[0] with arr2[0], arr1[1] with arr2[1], and so on. In this case, positions 2, 4, and 7 have matching values, so the result is 3. Using a For Loop ...
Read MoreAttach event to dynamic elements in JavaScript?
To attach events to dynamic elements in JavaScript, use event delegation with document.addEventListener(). This technique listens on a parent element (like document) and handles events from child elements that may be created dynamically. Why Event Delegation? When elements are added to the DOM dynamically (after the page loads), direct event listeners won't work because they weren't attached when the element was created. Event delegation solves this by using event bubbling. Basic Event Delegation Example Dynamic Events Example ...
Read MoreHow to detect the screen resolution with JavaScript?
To detect the screen resolution in JavaScript, you can use the window.screen object, which provides properties to access display dimensions and available screen space. Screen Properties Overview The window.screen object offers several useful properties: screen.width and screen.height - Total screen dimensions screen.availWidth and screen.availHeight - Available screen space (excluding taskbars) Basic Screen Resolution Detection Screen Resolution Detection ...
Read MoreAssigning function to variable in JavaScript?
In JavaScript, you can assign a function to a variable in several ways. This allows you to store functions for later use, pass them as arguments, or return them from other functions. Method 1: Function Expression The most common way is using a function expression, where you assign an anonymous function to a variable: Function Assignment // Assigning function to variable ...
Read MoreConvert number to tens, hundreds, thousands and so on - JavaScript
We are required to write a function that, given a number, say, 123, will output an array − [100, 20, 3] Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function. We can solve this problem by using a recursive approach. Example Following is the code − const num = 123; const placeValue = (num, res = [], factor = 1) => { if(num){ ...
Read MoreGet the last item from node list without using length property in JavaScript?
In JavaScript, you can get the last item from a NodeList without using the length property by leveraging CSS selectors, array methods, or destructuring. This is useful when you want to avoid calculating the length explicitly. Sample HTML Structure Let's use the following table as our example: John David Mike Method 1: Using CSS :last-child Selector The most direct approach is using CSS selectors to target the last element: ...
Read More