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 416 of 840
Explain the callback Promise.finally in JavaScript
The Promise.finally() method executes a callback function when a promise settles, regardless of whether it's fulfilled or rejected. This method is essential for cleanup operations and tasks that must run after promise completion. Syntax promise.finally(callback) Parameters callback: A function that executes when the promise settles. It receives no arguments and its return value is ignored. How It Works Unlike .then() and .catch(), the finally callback doesn't receive the promise's result or rejection reason. It simply indicates that the promise has completed. Example with Fetch API ...
Read MoreHow to create an ordered array from values that have an order number in JavaScript?
When working with arrays containing values and order numbers, you often need to sort them by their order and extract just the values. This is common when dealing with image galleries, menu items, or any data that needs custom ordering. Problem Setup Let's say we have an array of strings where each string contains a file path and an order number separated by a comma: const images = [ 'photo1.jpg, 0', 'photo2.jpg, 2', 'photo3.jpg, 1' ]; console.log("Original array:", images); Original array: [ 'photo1.jpg, 0', 'photo2.jpg, ...
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 MoreAdd object to array in JavaScript if name does not already exist?
To add objects to an array only if they don't already exist, you can use push() combined with find() to check for duplicates. This prevents adding duplicate entries based on specific criteria. Basic Example Here's how to add objects to an array if the name property doesn't already exist: var details = [{name: "John"}, {name: "David"}]; var addObject = ["Mike", "Sam", "John"]; // "John" already exists addObject.forEach(obj1 => { if (!details.find(obj2 => obj2.name === obj1)) { details.push({name: obj1}); } ...
Read MoreReverse word starting with particular characters - JavaScript
We need to write a JavaScript function that takes a sentence string and a character, then reverses all words starting with that specific character. For example, if we have the string: const str = 'hello world, how are you'; And we want to reverse words starting with 'h', the output should be: const output = 'olleh world, woh are you'; Notice that "hello" becomes "olleh" and "how" becomes "woh" because both start with 'h'. Solution We'll split the string into words, check each word's first character, and reverse those ...
Read MoreExplain 'dotAll' flag for regular expressions in JavaScript
The dotAll flag returns true or false depending upon if the s flag has been set in the regular expression. The s flag enables "dotAll" mode, where the dot . metacharacter matches any character, including newlines. What is the dotAll Flag? By default, the dot . in regular expressions matches any character except newlines. When the s flag is used, the dot can also match newline characters (, \r, etc.), making it truly match "any" character. Syntax regex.dotAll // Returns true if 's' flag is set, false otherwise Example: Checking dotAll Flag ...
Read MoreReorder array based on condition in JavaScript?
Let's say we have an array of objects that contains the scores of some players in a card game: const scorecard = [{ name: "Zahir", score: 23 }, { name: "Kabir", score: 13 }, { name: "Kunal", score: 29 }, { name: "Arnav", score: 42 }, { name: "Harman", score: 19 }, { name: ...
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 MoreHow to ignore using variable name as a literal while using push() in JavaScript?
To avoid using a variable name as a literal string when pushing objects to an array, use square brackets [variableName] for computed property names. This allows the variable's value to become the property key. The Problem with Literal Property Names When you use name: value in an object, "name" becomes a literal string property, not the variable's value: var name = "David"; var data = []; // This creates a property literally named "name" data.push({ name: "This will always be 'name' property" }); console.log(data); [ { name: 'This will always be 'name' ...
Read MoreReverse only the odd length words - JavaScript
We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them. Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or present at the end or beginning and followed or preceded by a space. Let's say the following is our string: const str = 'hello beautiful people'; The odd length words are: hello (5 characters - odd) beautiful (9 characters - odd) ...
Read More