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 Abhishek
68 articles
How to sort a set in JavaScript?
A Set is a JavaScript data structure that stores unique values in insertion order. Unlike arrays, Sets cannot be sorted directly because they don't have a sort() method. To sort a Set, we must convert it to an array, sort the array, and create a new Set from the sorted results. In this article, we'll learn different approaches to sort Sets in JavaScript using array conversion and the sort() method. Why Sets Cannot Be Sorted Directly Sets maintain insertion order and focus on uniqueness rather than sorting. They lack array methods like sort(), so we need an ...
Read MoreHow to sort an array of object by two fields in JavaScript?
An array of objects is an array that contains all the elements in the form of JavaScript objects. Here, we are said to use an array of objects with two fields that means, have to use objects with two elements as elements of the array. In this article, we are going to learn about the method of sorting the array of objects by two fields in JavaScript. We can simply use the sort() method of JavaScript to sort the elements of an array and give it a cmp or the comparator function to define the order in which we ...
Read MoreHow to sort an array on multiple columns using JavaScript?
An array with multiple columns is an array that contains multiple elements at each index position. In other words, it's an array of arrays where each sub-array represents a row with multiple column values that need to be sorted based on specific criteria. In this article, we'll learn how to sort arrays with multiple columns using JavaScript's sort() method combined with custom comparator functions. We'll explore different sorting strategies for various data types. Basic Syntax The fundamental approach uses a custom comparator function with the sort() method: array.sort((a, b) => { ...
Read MoreHow to sort element by numerical value of data attribute using JavaScript?
In JavaScript, you can sort DOM elements by their data attributes using different approaches. This is useful when you need to reorder elements based on numerical values stored in data attributes. This article demonstrates two methods to sort elements by numerical data attribute values: Using the getAttribute() method to access data attributes Using the dataset property for cleaner syntax Using the getAttribute() Method The getAttribute() method retrieves the value of any HTML attribute, including data attributes. This method works across all browsers and provides explicit attribute access. ...
Read MoreHow to solve the issue that arise while using performance.now() method in JavaScript?
The performance.now() method measures code execution time in milliseconds, but it has limitations when comparing algorithm efficiency. This article explores common issues and better alternatives for performance analysis. What is performance.now()? The performance.now() method returns a high-resolution timestamp representing the elapsed time since the page loaded. It's more precise than Date.now() for measuring performance. Syntax var startTime = performance.now(); // Your code here var endTime = performance.now(); var executionTime = endTime - startTime; Common Issues with performance.now() While useful for basic timing, performance.now() has several limitations: Small execution times: For ...
Read MoreHow to sort a map in JavaScript?
In JavaScript, Map is a data structure that stores values as key-value pairs. Maps preserve insertion order, but you can sort them by converting to an array, sorting, and creating a new Map. Syntax To sort a Map in JavaScript, use this approach: var sortedMap = new Map([...originalMap].sort(compareFunction)); Where compareFunction defines the sorting criteria for the key-value pairs. Sorting by Keys (Ascending) Here's how to sort a Map by keys in ascending order: Sorting Map by Keys (Ascending) ...
Read MoreHow to change the ID of the element using JavaScript?
Generally, the ID given to any element in an HTML document is unique and can be assigned to an element only once. However, it is possible to change that ID later using JavaScript while following the same rule of uniqueness for the new ID. In this article, we will learn how to change the ID of an element using JavaScript. JavaScript provides the id property to get the ID of any element as well as to change or add a new ID to any HTML element in the document. Syntax The following syntax shows how to use ...
Read MoreHow to change the shape of a textarea using JavaScript?
A textarea is an HTML element that provides a multi-line text input field with dynamic width and height. By default, textareas appear as rectangular boxes, but you can change their shape using JavaScript to modify CSS properties. In this article, we'll explore different methods to transform textarea shapes using JavaScript, including creating parallelograms and ellipses through CSS property manipulation. Using transform Property for Parallelogram Shape The transform property with skew() function can transform a rectangular textarea into a parallelogram by skewing it along the X or Y axis. Syntax element.style.transform = "skew(angle)"; ...
Read MoreHow to change the src attribute of an img element in JavaScript / jQuery?
There are different methods to change the path of an image given to the src attribute of an img element in HTML document using JavaScript and jQuery. Method of changing the src attribute of an img element using JavaScript: Use the src property in JavaScript. Methods of changing the src attribute of an img element using jQuery: Using attr() method Using prop() method Let us discuss each of the above listed methods of changing the src of an img element one by one in ...
Read MoreHow to change the text and image by just clicking a button in JavaScript?
In JavaScript, you can dynamically change text content and images by using the onclick event with button elements. This allows for interactive web pages where content updates in response to user actions. Let us explore how to change text and images individually using JavaScript with practical examples. Changing Text of an Element JavaScript provides two main properties for modifying element text content: innerText − Changes or retrieves the plain text content of an element, ignoring HTML tags. innerHTML − Changes or retrieves the HTML content of an element, including any HTML tags within it. ...
Read More