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 484 of 840
Add two array keeping duplicates only once - JavaScript
When working with two arrays, you often need to merge them while keeping only unique values. This operation combines arrays and removes duplicates in a single step. const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; We need to write a JavaScript function that takes two arrays and returns a new array with all duplicates removed (each element appears only once). Using Set with Spread Operator (Recommended) The most efficient approach uses ES6 Set to automatically remove duplicates: const arr1 = ...
Read MoreConverting array to object by splitting the properties - JavaScript
We have an array of string literals in which each element has a dash (-). The property key is present to the left of dash and its value to the right. A sample input array would look something like this: const arr = ["playerName-Kai Havertz", "age-21", "nationality-German", "position-CAM", "languages-German, English, Spanish", "club-Chelsea"]; We are required to write a function that splits these strings and form an object out of this array. Let's write the code. It will loop over the array splitting each string and feeding it into the new object. Using forEach Method ...
Read MoreReverse index value sum of array in JavaScript
Suppose we have an array of numbers like this: const arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7]; This array contains 10 elements, so the index of the last element is 9. We need to write a function that calculates the sum of each element multiplied by its reverse index position. The reverse index means we multiply each element by (array.length - currentIndex - 1). For our example: // Element at index 0: 3 * (10-0-1) = 3 * 9 = 27 // Element at index 1: 6 * ...
Read MoreHow to detect the screen resolution with JavaScript?
To detect the screen resolution in JavaScript, you can use the window.screen object, which provides properties to access display dimensions and available screen space. Screen Properties Overview The window.screen object offers several useful properties: screen.width and screen.height - Total screen dimensions screen.availWidth and screen.availHeight - Available screen space (excluding taskbars) Basic Screen Resolution Detection Screen Resolution Detection ...
Read MoreFlattening an array with truthy/ falsy values without using library functions - JavaScript
We need to write a JavaScript function that takes a nested array containing falsy values and returns a completely flattened array. This means converting a multi-dimensional array into a single-dimensional array while preserving all elements, including falsy values like false, null, and 0. For example, if the input is: const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; The output should be: [1, 2, 3, 4, 5, 5, false, 6, 5, 8, null, 6] Method 1: Using Recursive Function with Array.prototype Extension We can ...
Read MorePassing parameters to callback functions JavaScript
Callback functions in JavaScript can accept parameters just like regular functions. When passing a callback to another function, you can provide parameters that the callback will use when executed. Basic Callback with Parameters A callback function receives parameters when it's invoked by the calling function: Callback Parameters Run Example function add2(a) { ...
Read MoreSearching objects by value in JavaScript
When working with complex JavaScript objects, you often need to find all keys that contain a specific value. This is particularly useful for nested objects where the same value might appear at multiple levels. Consider this example object with nested structure: const obj = { "name": "Vivek Sharma", "occupation": "Software Engineer", "age": 23, "contacts": [{ "name": "Mukul Sharma", "occupation": "Software Engineer", ...
Read MoreSplit number into n length array - JavaScript
We are required to write a JavaScript function that takes in two numbers say m and n, and returns an array of size n with all the elements of the resulting array adding up to m. Let's write the code for this function − Example Following is the code − const len = 8; const sum = 5; const splitNumber = (len, sum) => { const res = []; for(let i = 0; i < len; i++){ res.push(sum / ...
Read MoreJavaScript array: Find all elements that appear more than n times
In JavaScript, you can find all elements that appear more than n times in an array using various approaches. This is useful for data analysis, finding duplicates, or filtering frequent items. Problem Overview Given an array of numbers or strings with repeated entries, we need to write a function that takes a positive integer n and returns all elements that appear more than or equal to n times. Using Map() for Frequency Counting The most efficient approach uses a Map to track element frequencies, then filters elements that meet our criteria: const arr = ...
Read MoreAssigning function to variable in JavaScript?
In JavaScript, you can assign a function to a variable in several ways. This allows you to store functions for later use, pass them as arguments, or return them from other functions. Method 1: Function Expression The most common way is using a function expression, where you assign an anonymous function to a variable: Function Assignment // Assigning function to variable ...
Read More