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
Javascript Articles
Page 453 of 534
How to combine 2 arrays into 1 object in JavaScript
Let's say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value. We will reduce the first array, at the same time accessing elements of the second array by index. This is a common pattern for creating objects from parallel arrays. Using Array.reduce() Method const keys = [ 'firstName', 'lastName', 'isEmployed', ...
Read MoreHTML5 Video Tag in Chrome - Why is currentTime ignored when video downloaded from my webserver?
The HTML5 video tag's currentTime property may not work properly in Chrome when your web server doesn't support HTTP range requests (byte-range serving). Chrome requires this feature for video seeking functionality. The Problem When you set currentTime on a video element, Chrome needs to jump to that specific time position in the video file. Without range request support, the browser cannot efficiently seek to arbitrary positions, causing currentTime to be ignored. Testing Your Web Server Check if your server supports byte-range requests using curl: curl --dump-header - -r0-0 http://yourserver.com/video.mp4 Look for this ...
Read MoreHow to Sort object of objects by its key value JavaScript
Let's say, we have an object with keys as string literals and their values as objects as well like this − const companies = { 'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'}, 'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'}, 'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'}, 'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'}, 'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'}, 'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'}, ...
Read MoreOrder an array of words based on another array of words JavaScript
When working with arrays in JavaScript, you may need to reorder an array of objects based on the order specified in another array. This is useful for sorting data according to custom priorities or sequences. Let's say we have an array of objects sorted by their id property: const unordered = [{ id: 1, string: 'sometimes' }, { id: 2, string: 'be' }, { id: 3, string: 'can' }, { ...
Read MoreMerge objects in array with similar key JavaScript
When working with arrays of objects in JavaScript, you might need to merge objects that share a common key. This is useful when consolidating data from multiple sources or restructuring existing data. Let's say we have the following array of objects where each object has an id and various heading properties: const arr = [ {id: 1, h1: 'Daily tests'}, {id: 2, h1: 'Details'}, {id: 1, h2: 'Daily classes'}, {id: 3, h2: 'Results'}, {id: 2, h3: 'Admissions'}, ...
Read MoreHow to remove certain number elements from an array in JavaScript
We are required to write a function that takes in an array of numbers and a number, and it should remove all the occurrences of that number from the array in-place. Let's explore different approaches to accomplish this task effectively. Method 1: Using Recursion with splice() We can use recursion to remove elements by finding and splicing them one by one. The recursive function continues until no more occurrences are found. const numbers = [1, 2, 0, 3, 0, 4, 0, 5]; const removeElement = (arr, element) => { if(arr.indexOf(element) !== ...
Read MoreGet the index of the nth item of a type in a JavaScript array
When working with JavaScript arrays, you might need to find the index of the nth occurrence of a specific value. This is useful for parsing data, finding patterns, or processing structured arrays. Problem Statement We need to write a function getIndex() that takes three parameters: an array arr, a value txt (string or number), and a number n. The function should return the index of the nth appearance of txt in arr. If txt doesn't appear n times, return -1. Using Array.reduce() Method The reduce() method provides an elegant solution by maintaining a counter and tracking ...
Read MoreSort a JavaScript array so the NaN values always end up at the bottom.
We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom. The array should contain all the normal numbers first followed by string literals and then followed by NaN numbers. We know that the data type of NaN is "number", so we can't check for NaN like !number && !string. Moreover, if we simply check the tautology and falsity of elements then empty strings will also satisfy the same condition which NaN or undefined satisfies. ...
Read MoreHow to Replace null with "-" JavaScript
In JavaScript, you often need to replace null, undefined, empty strings, and other falsy values with a default placeholder like "-". This is common when displaying data in tables or forms where empty values should show a dash instead of being blank. Understanding Falsy Values JavaScript considers these values as falsy: null, undefined, '' (empty string), 0, NaN, and false. We can use this to our advantage when replacing them. Method 1: Using Object.keys() and forEach() This approach iterates through all object keys and replaces falsy values in place: const obj = { ...
Read MoreGenerate colors between #CCCCCC and #3B5998 for a color meter with JavaScript?
We have to write a function that generates a random color between two given colors. Let's tackle this problem in parts − First → We write a function that generates a random number between two given numbers. Second → Instead of using the hex scale for random color generation, we will map the hex to 0 to 15 decimal scale and use that instead. ...
Read More