ES6 Javascript Array Functions

From Logic Wiki
Jump to: navigation, search


How to Remove Array Duplicates

Using Set

  1. First, we are creating a new Setby passing an array. Because Setonly allows unique values, all duplicates will be removed.
  2. Now the duplicates are gone, we’re going to convert it back to an array by using the spread operator ...
const array= ['aa',1,2,'aa',1,3];
const uniqueset = new set (array);  //-> Set {'aa',1,2,3 }
const backToArray = [...uniqueset];  //-> ['aa',1,2,3 ]

Array.from

Functionality is to convert two kinds of values to Arrays:

const array= ['aa',1,2,'aa',1,3];
Array.from(new Set(array));

Using filter

In order to understand this option, let’s go through what these two methods are doing: indexOf and filter.

indexOf

The indexOf method returns the first index it finds of the provided element from our array.

const array= ['aa',1,2,'aa',1,3];
array.indexOf('aa'); // 0
array.indexOf(1); // 1
array.indexOf(2); // 2
array.indexOf(3); // 5

filter

The filter() method creates a new array of elements that pass the conditional we provide. In other words, if the element passes and returns true, it will be included in the filtered array. And any element that fails or return false, it will be NOT be in the filtered array.

const array= ['aa',1,2,'aa',1,3];
array.filter((item,index) => array.indexOf(item) === index);
// Retrive duplicate values
array.filter((item,index) => array.indexOf(item) !== index);

Using reduce

The reduce method is used to reduce the elements of the array and combine them into a final array based on some reducer function that you pass.

In this case, our reducer function is checking if our final array contains the item. If it does, push that item into our final array. Otherwise, skip that element and return just our final array as is (essentially skipping over that element).

const array= ['aa',1,2,'aa',1,3];
array.reduce((unique, item) => { return unique.includes(item) ? unique : [...unique, item] },[]);

Searching for Array elements

find

Returns the first Array element for which the callback predicate returns true. If there is no such element, it returns undefined. Example:

[6, -5, 8].find(x => x < 0)
-5
[6, 5, 8].find(x => x < 0)
undefined

findIndex

Returns the index of the first element for which the callback predicate returns true. If there is no such element, it returns -1. Example:

[6, -5, 8].findIndex(x => x < 0)
1
[6, 5, 8].findIndex(x => x < 0)
-1

Array Methods

Following is the list of the methods of the Array object along with their description.

concat()

Returns a new array comprised of this array joined with other array(s) and/or value(s)

every()

Returns true if every element in this array satisfies the provided testing function.

filter()

Creates a new array with all of the elements of this array for which the provided filtering function returns true.

forEach()

Calls a function for each element in the array.

indexOf()

Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.

join()

Joins all elements of an array into a string.

lastIndexOf()

Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

map()

Creates a new array with the results of calling a provided function on every element in this array.

pop()

Removes the last element from an array and returns that element.

push()

Adds one or more elements to the end of an array and returns the new length of the array.

reduce()

Applies a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.

reduceRight()

Applies a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

reverse()

Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.

shift()

Removes the first element from an array and returns that element slice.

slice()

Extracts a section of an array and returns a new array.

some()

Returns true if at least one element in this array satisfies the provided testing function.

toSource()

Represents the source code of an object.

sort()

Sorts the elements of an array.

splice()

Adds and/or removes elements from an array.

toString()

Returns a string representing the array and its elements.

unshift()

Adds one or more elements to the front of an array and returns the new length of the array.

Array De-structuring

JavaScript supports de-structuring in the context of an array.

Example

var arr = [12,13] var[x,y] = arr console.log(x) console.log(y)