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 396 of 840
Convert object of objects to array in JavaScript
Let's say we have the following object of objects that contains rating of some Indian players, we need to convert this into an array of objects with each object having two properties namely name and rating where name holds the player name and rating holds the rating object. Following is our sample object: const playerRating = { 'V Kohli': { batting: 99, fielding: 99 }, 'R Sharma': { batting: 98, fielding: 95 }, ...
Read MoreDisplay all the numbers from a range of start and end value in JavaScript?
In JavaScript, you can display all numbers within a specified range using various methods. The most common approach is using a for loop to iterate through the range. Using for Loop Here's how to display numbers from a start value to an end value using a for loop: var startValue = 10; var endValue = 20; var result = []; function printAllValues(start, end) { for (var i = start; i < end; i++) { result.push(i); } } printAllValues(startValue, ...
Read MoreConvert JS array into an object - JavaScript
Converting a JavaScript array into an object is a common task in web development. This article shows different approaches to transform an array of objects into a single object using various JavaScript methods. Suppose we have an array of objects like this: const arr = [ {id: 1, name: "Mohan"}, {id: 2, name: "Sohan"}, {id: 3, name: "Rohan"} ]; console.log("Input array:", arr); Input array: [ { id: 1, name: 'Mohan' }, { id: 2, name: 'Sohan' }, { id: 3, name: 'Rohan' ...
Read MoreStop making form to reload a page in JavaScript
When a form is submitted, browsers reload the page by default. To prevent this behavior and handle form submissions with JavaScript, we need to stop the default form submission event. The Problem HTML forms automatically reload the page when submitted. This interrupts JavaScript processing and user experience: Method 1: Using preventDefault() with Form Submit Event The most reliable approach is using event.preventDefault() in the form's submit event handler: ...
Read MoreHow to Iterate Elements by ClassName in JavaScript?
To iterate elements by className in JavaScript, we use the getElementsByClassName() method. This method returns a live HTMLCollection of all elements in the document with the specified class name. In this article, we'll explore different approaches to iterate through elements that share the same class name, using practical examples with three div elements. getElementsByClassName() Overview The getElementsByClassName() method returns an HTMLCollection, which is array-like but not a true array. This collection updates automatically when elements are added or removed from the DOM. Approaches to Iterate Elements by ClassName Here are the most effective approaches to ...
Read MoreSorting objects by numeric values - JavaScript
Suppose we have an object like this: const obj = { key1: 56, key2: 67, key3: 23, key4: 11, key5: 88 }; We are required to write a JavaScript function that takes in this object and returns a sorted array like this: const arr = [11, 23, 56, 67, 88]; Here, we sorted the object values and placed them in an array. Method 1: Using Object.keys() and map() This approach extracts the keys, maps them ...
Read MoreJavaScript WebAPI File File.size Property
The JavaScript File WebAPI size property returns the size of a file in bytes. This read-only property is useful for validating file sizes before upload or displaying file information to users. Syntax file.size Return Value Returns a number representing the file size in bytes. Example File Size Property body { ...
Read MoreJoin every element of an array with a specific character using for loop in JavaScript
In JavaScript, you can join array elements with a specific character using a for loop. This approach gives you fine control over how elements are combined and what separators or wrappers to use. Problem Statement We need to create a function that takes an array and a string, then returns a new string where each array element is wrapped by the given string. For example: applyText([1, 2, 3, 4], 'a') should return 'a1a2a3a4a' Using for Loop Approach Here's how to solve this using a for loop to iterate through the array: ...
Read MoreRemove '0','undefined' and empty values from an array in JavaScript
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 MoreJavaScript WebAPI File File.type Property
The JavaScript File WebAPI file.type property returns the MIME type of a selected file. This property is essential for validating file types and handling different media formats in web applications. Syntax file.type Return Value Returns a string representing the MIME type of the file. If the type cannot be determined, it returns an empty string. Example: Getting File Type File Type Property ...
Read More