JavaScript Array Functions Cheat Sheet Part 2

  JavaScript Array Functions Cheat Sheet Part 2





find()

find() returns the value of the first element, that matches the test.

Syntax


    arr.find(callback(element[, index[, array]])[, thisArg])

Example


    [11056].find(val => val > 5// 10


findIndex()

findIndex() returns index of the first element, that matches test


Syntax


    arr.findIndex(callbackelement[, index[, array]] )[, thisArg])


Example


    [1456].findIndex(val => val > 5// 3


flat()

flat() creates a new array with sub-array elements flattened by specified depth.


Syntax


    const new_array = arr.flat([depth]);

Example


    [1, [2, [3, [4]]]].flat(2// [ 1, 2, 3, [4] ]


flatMap()

flatMap() creates a new array with sub-array elements flattened by specified depth.

Syntax


    var new_array = arr
    .flatMap(function callback(
        currentValue[, index[, array]]) {
        // return element for new_array
    } [, thisArg])


Example


    [[2], [4], [6], [8]].flatMap(val => val/2// [ 1, 2, 3, 4 ]


forEach()

forEach() executes provided function once for each array element.

Syntax


    arr.forEach(callback(currentValue [, index [, array]])[, thisArg])


Example


    [123].forEach(
        val => console.log(val)
    ) // 1 // 2 // 3


includes()

includes() determines if array includes a certain value


Syntax


    arr.includes(valueToFind[, fromIndex])


Example


    [ 123 ].includes(3// true

Thanks for reading .... 👏👏👏

JavaScript Array Functions Cheat Sheet Part 1

Reactions

Post a Comment

0 Comments