An Overview of Array Methods in JavaScript

Learn about how to manipulate arrays in JavaScript Image by PaweÅ‚ CzerwiÅ„ski from Unsplash Let’s create an array: var numbers = [1,2,3,4,5] Array elements are accessed using an index. The index starts from 0 . So if we want to access the first element, then we need to access using index 0. numbers[0] // 1 numbers[3] // …

Remove duplicate values of the array in Javascript.

Learn how to remove the duplicate values in the array Simple Way is using Set (which only contains unique value). var array = [1,2,3,4,1,2,3]; var unique = [… new Set(array)] ; //or var unique = Array.from (new Set(array) ); Another way is var elements = [1,2,3,4,1,2,3]; var uniqueArray = elements.filter(function(element, currentIndex) { return elements.indexOf(element) == …

Flatten an array in javascript

Learn how to convert a multi-dimensional array into single dimension array in javascript We can flatten an array to single dimension array using Array.flat(depth) method. But if we have multi-dimensional array then we need to find the depth of the Array, instead of finding depth we can solve the problem in two ways we can …

The Complete Reference for JavaScript Arrays

Learn all the methods available to Arrays in Javascript. Different ways of creating an array var array = []; var array = Array(); var array = Array(3); // array of length 3 var array = new Array() var array = new Array(3) // array of length 3 1. Array.from() The Array.from method creates a new shallow …

Getting a random item from an array

Let’s write a function to return a random element from an array. We can use Math.random() to generate a number between 0–1(inclusive of 0, but not 1) randomly. Then multiply the random number with the length of the array. floor to result to make it whole number . That’s it we find the random Index. Let’s …

Design a site like this with WordPress.com
Get started