JavaScript Array Functions Cheat Sheet Part 3

 JavaScript Array Functions Cheat Sheet Part 3




indexOf()

indexOf() returns the first index at which element can be found.

Syntax


    arr.indexOf(searchElement[, fromIndex])

Example


    [ 123 ].indexOf(3// 2


join()

join() returns string by concatenating all elements in array

Syntax


    arr.join([separator])

Example

     
    [ "x""y""z" ].join(" - "
    // "x - y - z"


keys() 

keys() returns Array Iterator that contains keys for each index

Syntax


    arr.keys()

Example


    ['a''b''c']
    .keys() // Array Iterator {  }
        .next() // { value: 0, done: false }
        .value // 0


lastIndexOf()

lastIndexOf() returns last index at which given element can be found.

Syntax

    
    arr.lastIndexOf(searchElement[, fromIndex])
    

Example


    12310].lastIndexOf(1// 3
    


map()

map() creates a new array with results of the provided function

Syntax


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


Example


        [234].map(val => val * 2)
        // [ 4, 6, 8 ]

pop()

pop() removes last element from array and returns that element.

Syntax


        arr.pop()

Example


    const arr = [123]
    arr.push(1)
    // returns: 4 // arr is [ 1, 2, 3, 4 ]


reduce()

reduce() executes a reducer function, resulting in a single output value.


Syntax


        arr.reduce(
            callback(
                accumulator,
                currentValue[, index[, array]]
            )[, initialValue])
        


Example


        ['a''b''c'].reduce(
            (acccurr=> acc + curr'd'
        ) // "dabc"


reduceRight()

reduceRight() executes a reducer function from right to left, resulting in single output value


Syntax


        arr.reduceRight(
            callback(accumulator,
                currentValue[, index[, array]])
            [, initialValue]
        )

Example


        [ 123 ].reverse() // [ 3, 2, 1 ]

Thanks for reading... 👏👏👏 


JavaScript Array Functions Cheat Sheet Part 2


Reactions

Post a Comment

0 Comments