Array Methods Part 2

concat()

Allows you to concatenate two arrays into one.

define two arrays

let  numsOne = [1, 3, 5, 7, 9];
let numsTwo  = [2, 4, 6, 8];

concatenate numsOne and numsTwo

let cocatenated =  numsOne.concat(numsTwo);

display concatenated array

console.log(concatenated);
Returns: Array [ 1,  3,  5,  7,  9,  2,  4,  6,  8]

sort()

This method Sorts any given array. the sort occurs in-place.

Sort concatenated array

concatenated.sort();
console.log(concatenated);
Returns: Array [  1,  2,  3,  4,  5,  6,  7,  8,  9]

entries()

This method returns a new object, an Array Iterator, which contains a key/value pair for each index/element in the array.

let numeros = ['zero', 'uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete', 'ocho', 'nueve'];

Convert numeros into an iterator

let myIterator = numeros.entries();

display first two pairs in myIterator

console.log(myIterator.next().value);
returns: Array [ 0,  "zero"]

console.log(myIterator.next().value);
returns: Array [  1,  "uno",]

Looping thru --> myIterator

let result = myIterator.next(); 
 while ( !result.done ) 
  {
     console.log(result.value);
     result = myIterator.next();
  }

Returns:  Array [ 0,  "zero",]
          Array [  1,  "uno",]
          Array [  2,  "dos",]
          Array [  3,  "tres",]
          Array [  4,  "cuatro",]
          Array [  5,  "cinco",]
          Array [  6,  "seis",]
          Array [  7,  "siete",]
          Array [  8,  "ocho",]
          Array [  9,  "nueve",]
          Object {  "done": true,  "value": undefined,
}

every()

This method checks if every value in any given array fulfills a given criteria. it returns a boolean.

Test for the following condition: All array elements < 50.

let nums = [10, 20, 15, 30, 35, 25, 40, 47];
if (nums.every( (n) => { return  n < 50; })) 
{ console.log("All elements in array are < than 50!!"); }
else
{ console.log("Not all elements are < than 50!!") }

Returns:  "All elements in array are < than 50!!"

Or use a function for the criteria/condition to be tested on each array element,

see isCurrentValueLessThan_50

const isCurrentValueLessThan_50 = (value) => value < 50;

if (nums.every( isCurrentValueLessThan_50) )
{ console.log("All elements in array are < than 50!!"); }
else
{ console.log("Not all elements are < than 50!!") }

Returns:  "All elements in array are < than 50!!"

filter()

This method checks for all elements that pass a given condition, and then it creates a new array with those elements.

get all array elements , where element.length > 4

let numeros = ['zero', 'uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete', 'ocho', 'nueve'];
const lengthGreaterThan_4 = numeros.filter( numero => numero.length > 4);
console.log(lengthGreaterThan_4);

Returns:  Array [  "cuatro",  "cinco",  "siete",  "nueve",]

find()

This method returns the first array element that fulfills a given condition.

## get first array element > 10
let nums = [10, 20, 15, 30, 35, 25, 40, 47];
let numFound = nums.find(element => element > 10);
console.log(numFound);

Returns: 20

lastIndexOf()

This method returns the index of the last occurrance of a given array element.

### get the index of last occurrance of 20
let array = [10, 20, 15, 30, 35, 25, 20, 40, 47, 20, 25, 30];
console.log(array.lastIndexOf(20));
Returns: 9

map()

populates a new array with the results of a called function that processes each element in the array.

## Get the square of each array element
let array = [10, 20, 15, 30, 35, 25, 20, 40, 47];
let mapArray = array.map(element => element * element);
console.log(mapArray);

Returns:
Array [  100,  400,  225,  900,  1225,  625,  400,  1600,  2209]

toString

this method converts any given array object to a string.

##convert array arr1 to a string 

let arr1 = [10, 20, 15, 30, 35, 25, 20, 40, 47];
let arrayToString = arr1.toString();
console.log (arr1.toString());

Returns: "10,20,15,30,35,25,20,40,47"

This concludes my article on some of the most common array methods.

Live code

Let's Connect

#Next article will be: functions in Javascripts