Skip to content

Instantly share code, notes, and snippets.

@gn-bcampbell
Created June 19, 2023 07:40
Show Gist options
  • Select an option

  • Save gn-bcampbell/52301bd4c96a38e8e844b697002bc6ce to your computer and use it in GitHub Desktop.

Select an option

Save gn-bcampbell/52301bd4c96a38e8e844b697002bc6ce to your computer and use it in GitHub Desktop.

Revisions

  1. gn-bcampbell created this gist Jun 19, 2023.
    56 changes: 56 additions & 0 deletions arrayMethods.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    let arr = [1,2,3,4,5,6,7]

    // IF I WANT...

    // ---- Mutate the original array ---- //

    // Add to
    arr.push() // end
    arr.unshift() //start

    // Remove from
    arr.pop() //end
    arr.shift() //start
    arr.splice() //any index

    //Others
    arr.reverse()
    arr.sort()
    arr.fill();


    // ---- Create a new Array ---- //

    arr.map() // from the original
    arr.filter() // with a condition
    arr.slice() // portion of original
    arr.concat() //adding original to other

    arr.flat() //flatten the original
    arr.flatMap()

    // ---- An array index ---- //
    arr.indexOf() // based on value
    arr.findIndex() //based on test condition


    // ---- An array element ---- //
    arr.find() // based on test condition

    // ---- Know if array includes ---- //
    arr.includes() //based on value
    arr.some() //based on test condition

    // ---- A new string ---- //
    arr.join() // based on separator string

    // ---- To transform to value ---- //
    arr.reduce() // boil down to single value

    // ---- To just loop array ---- //
    arr.forEach() // based on callback