Array, Array methods
Tags: beginners, javascript, array, method
Published: 2021-10-08 12:16:20

##_ARRAY_

An array is a special variable, which can hold more than one value at a time.

To create a new array:
    
    const array = new Array();
 
__or__ 

    const array = [];
    
Arrays can contain different types of values

    let array = [
      "John",
      {
        type: "JS",
        age: 36
      },
      true,
      function() {
        console.log('Hello, I am John');
      }
    ];
    console.log(array);
    console.log(array[0]);
    console.log(array[1].type);
    console.log(array[2]);
    array[3]();

Result:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4esn604wnswnalg0k9x0.png)
    
    
####_FOREACH ()_
The forEach() method calls a function once for each element in an array, in order.
forEach() is not executed for array elements without values.

    arr.forEach(function callback(currentValue, index, array) {
        //your iterator
    }[, thisArg]);
    index, array, thisArg - optional

#####ex.
    const arr = ['a', 'b', 'c'];
    arr.forEach(element => console.log(element)); 
    //expected output a, b, c

#####ex.
    const numbers = [65, 44, 12, 4];
    numbers.forEach(function myFunction(item, index, arr) {
      arr[index] = item * 10;
    }) 
    console.log(numbers) // 650, 440, 120, 40

####_PUSH()_

__push()__ adds new items to the end of an array.
__push()__ changes the length of the array and returns the new length.

    let arr = [ 1, 2, 3, 4,];
    arr.push(5, 6);
    console.log(arr);
    // result [ 1, 2, 3, 4, 5, 6 ]

####_UNSHIFT()_

__unshift()__ adds items at the beginning of an array.

    let arr = [ 1, 2, 3, 4,];
    arr.unshift(5, 6);
    console.log(arr);
    // result [ 5, 6, 1, 2, 3, 4 ]

####_SHIFT()_

__shift()__ removes the first item of an array.

    let arr = [ 1, 2, 3, 4,];
    arr.shift();
    console.log(arr);
    // result [ 2, 3, 4 ]

####_POP()_

__pop()__ removes the last element of an array.

    let arr = [ 1, 2, 3, 4,];
    arr.shift();
    console.log(arr);
    // result [ 1, 2, 3 ]

####_SPLICE()_

__splice()__ adds and/or removes array elements.

    array.splice(index, howmany, item1, ....., itemX)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6qh11x1hduupif7dj337.png)

####_CONCAT()_

__concat()__ concatenates (joins) two or more arrays. __concat()__ does not change the existing arrays, but returns a new array, containing the values of the joined arrays. 

    let arr = [ 1, 2, 3,];
    let arr1 = arr.concat(10);
    console.log(arr1);
    // result [ 1, 2, 3, 10 ]

####_INCLUDES()_ _INDEXOF()_
_includes()_ determines whether an array contains a given element and returns either true or false.
_indexOf()_ searches an array for a specified item and returns its position. _indexOf()_ returns -1 if the item is not found.

    let arr = [ 1, 2, 3, 4, 10, 15];
    console.log(arr.indexOf(3));
    console.log(arr.includes(15));
    // result 2 true

####_MAP()_
The _map()_ method creates a new array with the results of calling a function for every array element.
The _map()_ method calls the provided function once for each element in an array, in order.
_map()_ does not execute the function for empty elements.
_map()_ does not change the original array.

#####ex.
    const persons = [
      {firstname : "Malcom", lastname: "Reynolds"},
      {firstname : "Kaylee", lastname: "Frye"},
      {firstname : "Jayne", lastname: "Cobb"}
    ];
    persons.map(function getFullName(item) {
      return [item.firstname,item.lastname].join(" ");
    })

####_FILTER()_
The _filter()_ method creates an array filled with all array elements that pass a test (provided by a function).
_filter()_ does not execute the function for empty array elements.
_filter()_ does not change the original array.

#####ex.
    const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    const result = words.filter(word => word.length > 6);
    console.log(result);

####_REDUCE()_
__reduce()__ executes a reducer function for each value of an array.

    let arr = [ 1, 2, 3, 4, 10, 15];
    let sum = arr.reduce((prev, item) => {
        return item + prev
    }, 0);
    console.log(sum);
    // result 35