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 364 of 840
Get max value per key in a JavaScript array
When working with arrays of objects in JavaScript, you might need to find the object with the maximum value for a specific property within each group. This is a common task when analyzing data grouped by categories. Consider this array of fruit data: const arr = [ {a:1, b:"apples"}, {a:3, b:"apples"}, {a:4, b:"apples"}, {a:1, b:"bananas"}, {a:3, b:"bananas"}, {a:5, b:"bananas"}, {a:6, b:"bananas"}, {a:3, b:"oranges"}, ...
Read MoreTrigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?
By default, the click event fires when you press and then release the mouse button. To trigger an event immediately when the mouse button is pressed down, use the mousedown event instead. The Difference Between click and mousedown The click event requires a complete click cycle (press down + release), while mousedown fires immediately when the button is pressed. Using mousedown Event Here's how to trigger an event immediately on mouse press: Mousedown Event Example ...
Read MoreJoin in nested array in JavaScript
Joining elements from nested arrays in JavaScript requires flattening the array structure first. This article explores different approaches to join nested array elements with a semicolon separator. The Problem Consider this nested array: const arr = ['zero', ['one', 'two', 'three', ['four', ['five', 'six', ['seven']]]]]; console.log("Original nested array:", arr); Original nested array: [ 'zero', [ 'one', 'two', 'three', [ 'four', [Array] ] ] ] We need to extract all elements and join them with semicolons to get: zero;one;two;three;four;five;six;seven; Using flat() Method (Modern Approach) The flat(Infinity) method flattens ...
Read MorePreparing encoding and decoding algorithms for shortening URLs in JavaScript
URL shortening services like bit.ly and TinyURL take long URLs and convert them into shorter, more manageable links. This process involves encoding the original URL into a compact format and later decoding it back when accessed. To implement a basic URL shortening system in JavaScript, we need two main functions: encrypt() → takes the original URL and returns a shortened unique URL decrypt() → takes the shortened URL and converts it back to the original URL Basic Implementation Using Base64 Encoding Here's a simple approach using ...
Read MoreRearranging digits to form the greatest number using JavaScript
Problem We are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number. Approach To create the maximum number, we need to arrange the digits in descending order. We can convert the number to a string, split it into individual digits, sort them in descending order, and join them back. Example Following is the code − const num = 149; const maxRedigit = function(num) { if(num < 100 || num > 999) ...
Read MoreIs the digit divisible by the previous digit of the number in JavaScript
Problem We need to write a JavaScript function that takes a number and checks if each digit is divisible by the digit on its left. The function returns an array of booleans, where the first element is always false since there's no digit before the first one. Example Let's check the number 73312 digit by digit: const num = 73312; const divisibleByPrevious = (n = 1) => { const str = n.toString(); const arr = [false]; for(let i = 1; ...
Read MoreHow to join JavaScript array of string
We need to write a JavaScript function that joins an array of strings, replaces all whitespaces with dashes "-", and returns the formatted string. For example: If the array is − const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; Then the output should be − const output = "QA-testing-promotion-Twitter-Facebook-Test"; Method 1: Using Loop and String Concatenation This approach joins the array elements and processes each character to replace spaces with dashes: const arr = ["QA testing promotion ", " Twitter ", "Facebook ", ...
Read MoreFinding closest pair sum of numbers to a given number in JavaScript
We need to write a JavaScript function that takes an array of numbers and a target number, then returns the pair of numbers from the array whose sum is closest to the target. The function should find two different numbers from the original array that, when added together, produce a sum nearest to the specified target value. Basic Approach Using Nested Loops Here's a straightforward solution using nested loops to check all possible pairs: const arr = [1, 2, 3, 4, 5, 6, 7]; const num = 14; const closestPair = (arr, target) => ...
Read MoreHow to remove duplicate property values in array – JavaScript?
In JavaScript, you may need to remove duplicate values from array properties within an array of objects. This is common when working with data that contains repeated values that should be unique. Problem Example Consider an array of student objects where some students have duplicate marks in their studentMarks array: var details = [ { studentName: "John", studentMarks: [78, 98] }, { ...
Read MoreRemove item from a nested array by indices in JavaScript
Suppose we have a nested array of objects like this: const arr = [ { value: 'some value' }, { array: [ { value: 'some value' }, { array: [ ...
Read More