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
Front End Technology Articles
Page 382 of 652
What is the importance of _.initial() function in JavaScript?
The _.initial() function is part of the Underscore.js library that returns all elements of an array except the last n elements. It's useful for removing elements from the end of an array without modifying the original array. Syntax _.initial(array, n); Parameters array - The input array from which to extract elements. n (optional) - Number of elements to exclude from the end. Default is 1. Return Value Returns a new array containing all elements except the last n elements. Example: Basic Usage ...
Read MoreAdding two values at a time from an array - JavaScript
Let's say, we are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of two consecutive elements from the original array. For example, if the input array is − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; Then the output should be − const output = [9, 90, 26, 4, 14]; Example Following is the code − const arr = [3, 6, 3, 87, 3, 23, 2, 2, 6, 8]; ...
Read MoreHow to remove the 0th indexed element in an array and return the rest of the elements in JavaScript?
Arrays in JavaScript are zero-indexed data structures where elements can be accessed using their index positions. The first element is at index 0, the second at index 1, and so on. const colors = ["Green", "Maroon", "Blue"]; // Index: 0 1 2 This article explores different methods to remove the first element (at index 0) from an array and return the remaining elements. Using the shift() Method The shift() method removes the first element from an array ...
Read MoreHow to find the common elements between two or more arrays in JavaScript?
In JavaScript, finding common elements between two or more arrays is a frequent requirement in web development. Arrays are objects that store multiple values, and there are several efficient methods to identify shared elements across them. Using for Loop The traditional approach uses nested for loops to compare each element of one array with every element of another array. This method works by creating an empty array and pushing matching elements into it. Example 1 Here's how to find common elements between two numeric arrays: Common elements between ...
Read MoreHow to get the first n values of an array in JavaScript?
In JavaScript, getting the first n elements from an array is a common operation. There are several built-in methods to accomplish this efficiently. First n values means extracting elements from index 0 to index n-1. For example, if n=3, you get elements at positions 0, 1, and 2. Using the slice() Method (Recommended) The slice() method is the most common and efficient way to get the first n elements. It creates a new array without modifying the original. Syntax array.slice(0, n) Example 1: Basic Usage ...
Read MoreCounting the clusters of positive numbers - JavaScript Arrays
Let's say, we have an array of numbers like this − const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; We are required to write a JavaScript function that counts the consecutive groups of non-negative (positives and 0) numbers in the array. Like here we have consecutive non-negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from index 9 to end of array forms the second group. Therefore, for this array, the function should return 2. How It Works ...
Read MoreHow to invert an object in JavaScript?
Inverting an object in JavaScript means swapping its keys and values, so that {name: "John", age: 25} becomes {"John": "name", "25": "age"}. This is useful for creating lookup tables or reverse mappings. There are several ways to invert objects in JavaScript, from using libraries like Underscore.js to native JavaScript methods. Using Underscore.js _.invert() The _.invert() function from Underscore.js provides a simple way to swap object keys and values. Syntax _.invert(object) Parameters: object − The object to invert Return Value: A new object with keys and values ...
Read MoreWhat is the relation between 'null' and '0' in JavaScript?
In JavaScript, the relationship between null and 0 creates a seemingly contradictory behavior that confuses many developers. While null is neither greater than nor equal to 0 in equality checks, it evaluates as greater than or equal to 0 in comparison operations. Understanding null and 0 null is a primitive value representing an intentional absence of value, while 0 is a number. The confusing behavior occurs due to JavaScript's type coercion rules, which differ between equality (==) and comparison (>, =,
Read MoreCode to find the center of an array without using ES6 functions - JavaScript
We are required to write an array function midElement() that returns the middlemost element of the array without accessing its length property and without using any kind of built-in loops. If the array contains an odd number of elements, we return the one middlemost element, or if the array contains an even number of elements, we return an array of two middlemost elements. How It Works The solution uses recursion to count array elements by incrementing an index until reaching undefined, then calculates the middle position(s) based on whether the count is odd or even. Example ...
Read MoreHow to remove the last digit of a number and execute the remaining digits in JavaScript?
In JavaScript, you can remove the last digit from a number using different approaches. The most common methods involve string manipulation or mathematical operations like bitwise operations. When working with numbers, you typically need to convert the number to a string to use string methods, or use mathematical operations to avoid type conversion altogether. Method 1: Using String substring() The substring() method extracts characters from a string between two specified indices. Syntax str.substring(startIndex, endIndex) To remove the last digit, use str.substring(0, str.length - 1) where the first parameter is the starting index ...
Read More