Javascript Articles

Page 448 of 534

Sorting by 'next' and 'previous' properties (JS comparator function)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 422 Views

When working with linked data structures in JavaScript, you may encounter objects that reference each other through 'next' and 'previous' properties. This tutorial demonstrates how to sort such objects into their correct sequential order. Problem Statement Consider an array of objects representing pages of a website, where each object has: An id property for unique identification A next property pointing to the next page's id (unless it's the last page) A previous property pointing to the previous page's id (unless it's the first page) These objects are randomly positioned in the array, and we need ...

Read More

Convert object of objects to array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

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 More

Stop making form to reload a page in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 8K+ Views

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 More

How to convert array to object in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 434 Views

Converting arrays to objects is a common task in JavaScript. There are several approaches depending on your data structure and requirements. Method 1: Array of Arrays to Objects with Alphabetic Keys Let's convert a nested array structure into objects with keys as English alphabet letters: const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]; const dataArr = data.map(arr => { return arr.reduce((acc, cur, index) => ({ ...acc, [String.fromCharCode(97 + index)]: ...

Read More

How to set attribute in loop from array JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Let's say we are required to write a function that takes in an array and changes the id attribute of first n divs present in a particular DOM according to corresponding values of this array, where n is the length of the array. We will first select all divs present in our DOM, iterate over the array we accepted as one and only argument and assign the corresponding id to each div. Basic Example Here's the HTML structure we'll work with: Setting Attributes in Loop ...

Read More

Why does Array.map(Number) convert empty spaces to zeros? JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 454 Views

When using Array.map(Number) on a string containing spaces, JavaScript converts empty spaces to zeros. This happens due to how the Number() constructor handles string-to-number conversion. The Problem Let's examine this behavior with a practical example: const digify = (str) => { const parsedStr = [...str].map(Number) return parsedStr; } console.log(digify("778 858 7577")); [ 7, 7, 8, 0, 8, 5, 8, 0, 7, 5, 7, 7 ] Notice how the spaces in the string are converted to 0 instead ...

Read More

Clearing localStorage in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 568 Views

localStorage is a web storage API that persists data in the browser until explicitly cleared. Here are two effective methods to clear localStorage in JavaScript. Method 1: Using clear() Method The clear() method removes all key-value pairs from localStorage at once. This is the most efficient approach. // Store some data first localStorage.setItem("name", "John"); localStorage.setItem("age", "25"); localStorage.setItem("city", "New York"); console.log("Before clearing:", localStorage.length); // Clear all localStorage localStorage.clear(); console.log("After clearing:", localStorage.length); Before clearing: 3 After clearing: 0 Method 2: ...

Read More

Display array items on a div element on click of button using vanilla JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 4K+ Views

To display array items in a div element when a button is clicked, we need to iterate through the array and append each element to the target div. This is commonly done using JavaScript's forEach() method or a simple loop. Basic Approach The core concept involves selecting the target div using getElementById() and updating its innerHTML property with array elements: const myArray = ["stone", "paper", "scissors"]; const embedElements = () => { myArray.forEach(element => { document.getElementById('result').innerHTML += ...

Read More

Filtering of JavaScript object

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

Filtering JavaScript objects allows you to extract specific key-value pairs based on certain criteria. This is useful when working with large datasets or when you need to display only relevant information. Problem Statement We need to create a function that takes an object and a search string, then filters the object keys that start with the search string and returns a new filtered object. Example: Basic Object Filtering const obj = { "PHY": "Physics", "MAT": "Mathematics", "BIO": "Biology", "COM": "Computer Science", "SST": "Social Studies", ...

Read More

Reverse a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 7K+ Views

Our aim is to write a JavaScript function that takes in a number and returns its reversed number. For example, reverse of 678 is: 876 There are multiple approaches to reverse a number in JavaScript. Let's explore the most common methods. Method 1: Using String Conversion The most straightforward approach converts the number to a string, reverses it, and converts back to a number: const num = 124323; const reverse = (num) => parseInt(String(num) .split("") .reverse() .join(""), 10); console.log(reverse(num)); ...

Read More
Showing 4471–4480 of 5,340 articles
« Prev 1 446 447 448 449 450 534 Next »
Advertisements