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
Javascript Articles
Page 461 of 534
Search and update array based on key JavaScript
In JavaScript, you often need to merge data from two arrays based on a common property. This is useful when you have reference data in one array and need to enrich objects in another array. Consider these two arrays: let arr1 = [ {"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSITION":"GMH"} ]; let arr2 = [ {"EMAIL":"test1@stc.com", "POSITION":"GM"}, {"EMAIL":"test2@stc.com", "POSITION":"GMH"}, {"EMAIL":"test3@stc.com", "POSITION":"RGM"}, {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ]; console.log("Original arr2:", arr2); ...
Read MoreLoop backward in array of objects JavaScript
When working with arrays of objects in JavaScript, you may need to iterate through them in reverse order. This is useful for tasks like displaying data from newest to oldest or building strings that reverse the original order. We have an array of objects like this: let data = [ {id:1, Name: "Abe", RowNumber: 1 }, {id:2, Name: "Bob", RowNumber: 2 }, {id:3, Name: "Clair", RowNumber: 3 }, {id:4, Name: "Don", RowNumber: 3.0 }, {id:5, Name: "Edna", ...
Read MoreHow to count digits of given number? JavaScript
The requirements here are simple, we are required to write a JavaScript function that takes in a number and returns the number of digits in it. For example − The number of digits in 4567 is 4 The number of digits in 423467 is 6 The number of digits in 457 is 3 Let's explore different approaches to count digits in JavaScript. Method 1: Using Recursive Approach This method uses recursion to divide the number by 10 until it reaches zero: const num = 2353454; const digits = (num, count = ...
Read MoreAccess previously iterated element within array.map in JavaScript?
In JavaScript, you can access previously iterated elements within array.map() using the index parameter. The map() method provides both the current element and its index, allowing you to reference earlier elements in the array. Let's say the following is our array: var details = [ {subjectId:110, subjectName: 'Java' }, {subjectId:111, subjectName: 'Javascript' }, {subjectId:112, subjectName: 'MySQL' }, {subjectId:113, subjectName: 'MongoDB' } ]; Using Index to Access Previous Elements The key is to use the index parameter in the map() callback to access ...
Read MoreJavaScript get the length of select options(dropdown)?
In JavaScript, you can get the length of select options (dropdown) using different methods. This is useful for validation, dynamic content management, or determining how many options are available. HTML Structure Let's start with a basic select element: John Mike Sam David Carol Using Vanilla JavaScript The most straightforward approach uses the options.length property: ...
Read MoreJavaScript Sum function on the click of a button
In JavaScript, you can create a sum function that executes when a button is clicked. This involves defining a function and attaching it to the button's onclick event. Basic Button Setup Here's how to create a button that calls a function with a parameter: Sum When clicked, this button calls the addTheValue() function with the value 10 as a parameter. Complete Example Sum Function Example Adding 10 each ...
Read MoreaddEventListener() not working more than once with a button in JavaScript?
The addEventListener() method works multiple times by design. If it seems to work only once, the issue is usually in your event handler function or how you're setting up the listener. Common Problem: Removing the Element A frequent mistake is removing or replacing the button element inside the event handler, which destroys the event listener: addEventListener Problem Bad Example - Works Once ...
Read MoreJavaScript How to get all 'name' values in a JSON array?
In JavaScript, extracting specific values from a JSON array is a common task. When working with nested objects, you can use the map() method to iterate through the array and extract the desired properties. Sample JSON Data Let's consider the following JSON array with customer information: var details = [ { "customerDetails": [ { "customerName": "John ...
Read MoreBest way to find two numbers in an array whose sum is a specific number with JavaScript?
Finding two numbers in an array that sum to a target value is a common programming problem. We'll explore an efficient solution using JavaScript. Problem Statement Given an array of numbers, find two elements whose sum equals a specific target value: var numbers = [10, 3, 40, 50, 20, 30, 100] Target sum = 80 We need to find pairs like (50, 30) where 50 + 30 = 80. Efficient Solution Using Hash Map The optimal approach uses a hash map to store visited numbers and their complements, achieving O(n) time complexity: ...
Read MoreRemove the whitespaces from a string using replace() in JavaScript?
In JavaScript, the replace()
Read More