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
Articles by AmitDiwan
Page 464 of 840
Finding the length of a JavaScript object
JavaScript objects don't have a built-in length property like arrays. However, there are several methods to find the number of properties in an object. Suppose we have an object like this: const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 }; We need to count the number of properties in this object. Using Object.keys() (Recommended) The most common and reliable method is Object.keys(), which returns an array of the object's property names: ...
Read MoreHow to remove a class name from an element with JavaScript?
To remove a class name from an element with JavaScript, you can use the classList.remove() method. This method provides a clean and efficient way to manipulate CSS classes dynamically. Syntax element.classList.remove('className'); Example .newStyle { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; width: 100%; padding: 25px; background-color: rgb(147, 80, 255); color: white; ...
Read MoreFilter the properties of an object based on an array and get the filtered object JavaScript
In JavaScript, you often need to filter an object's properties based on specific criteria. This article shows how to create a filtered object containing only the properties whose keys appear in a given array. Problem Statement We need to write a function that takes an object and an array of string literals, then returns a new object containing only the key-value pairs where the key exists in the array. For example: If the object is {"a": [], "b": [], "c": [], "d": []} and the array is ["a", "d"], the output should be: {"a": [], ...
Read MoreSorting an array of objects by property values - JavaScript
In JavaScript, you can sort an array of objects by property values using the Array.sort() method with a custom comparison function. This is particularly useful when working with data structures like product catalogs, user lists, or any collection of objects. Sample Data Let's work with this array of home objects: const homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", ...
Read MoreHow to add an active class to the current element with JavaScript?
Adding an active class to the current element allows you to highlight the selected item in navigation menus, tabs, or button groups. This technique uses JavaScript event listeners to dynamically manage CSS classes. Complete Example .btn { border: none; outline: none; padding: 10px 16px; background-color: #6ea2f0; cursor: pointer; color: white; ...
Read MoreSort the numbers so that the even numbers are ahead JavaScript
We have an array of numbers that contains some positive and negative even and odd numbers. We need to sort the array in ascending order but with a special requirement: all even numbers should appear before any odd number, and both groups should be sorted internally in ascending order. For example, if we have an array like [-2, 3, 6, -12, 9, 2, -4, -11, -8], the result should be [-12, -8, -4, -2, 2, 6, -11, 3, 9]. How It Works The solution uses a custom comparator function that: Checks if numbers are even ...
Read MoreHow to create a custom function similar to find() method in JavaScript?
Let's say we have the following records of studentId and studentName and want to check a specific student name: const studentDetails = [ { studentId: 101, studentName: "John" }, { studentId: 102, studentName: "David" }, { studentId: 103, ...
Read MoreFunction that parses number embedded in strings - JavaScript
JavaScript's built-in functions like parseInt() and parseFloat() only parse numbers from the beginning of a string, stopping when they encounter non-numeric characters. When you need to extract all digits from anywhere within a string, you need a custom solution. The Problem with Built-in Methods Standard parsing methods fail with embedded numbers: console.log(parseInt('454ffdg54hg53')); // 454 (stops at first non-digit) console.log(parseFloat('12.34abc56.78')); // 12.34 (stops at 'a') 454 12.34 Method 1: Loop Through Characters This approach iterates through each character, extracting only digits: const numStr = '454ffdg54hg53'; ...
Read MoreCapitalize letter in a string in order and create an array to store - JavaScript
We are required to write a JavaScript function that takes in a string and turns it into a Mexican Wave i.e. resembling string produced by successive capital letters in every word. For example, if the string is: const str = 'edabit'; Then the output should be the following with successive single capital letters: const output = ["Edabit", "eDabit", "edAbit", "edaBit", "edabIt", "edabiT"]; Method 1: Using String Prototype Extension This approach extends the String prototype with a custom replaceAt method: const str = 'edabit'; const replaceAt = function(index, ...
Read MoreHow to borrow methods in JavaScript?
Method borrowing allows an object to use another object's method by temporarily setting the context using call(), apply(), or bind(). Understanding Method Borrowing In JavaScript, methods are just functions attached to objects. When you borrow a method, you're calling a function from one object but executing it in the context of another object using this binding. Using call() Method The call() method invokes a function with a specified this context and individual arguments. Method Borrowing with call() ...
Read More