Js Array Cheat Sheet

From WikiOD

Here is the cheatsheat of Js Array and basic guide on how to use Js Array

Arrays[edit | edit source]

list = [a,b,c,d,e]

{: .-setup}

list[1]                 // → b
list.indexOf(b)         // → 1

Subsets[edit | edit source]

Immutable[edit | edit source]

list.slice(0,1)         // → [a        ]
list.slice(1)           // → [  b,c,d,e]
list.slice(1,2)         // → [  b      ]

Mutative[edit | edit source]

re = list.splice(1)     // re = [b,c,d,e]  list == [a]
re = list.splice(1,2)   // re = [b,c]      list == [a,d,e]

Adding items[edit | edit source]

Immutable[edit | edit source]

list.concat([X,Y])      // → [_,_,_,_,_,X,Y]

Mutative[edit | edit source]

list.push(X)            // list == [_,_,_,_,_,X]
list.unshift(X)         // list == [X,_,_,_,_,_]
list.splice(2, 0, X)    // list == [_,_,X,_,_,_]

Inserting[edit | edit source]

// after -- [_,_,REF,NEW,_,_]
list.splice(list.indexOf(REF)+1, 0, NEW))
// before -- [_,_,NEW,REF,_,_]
list.splice(list.indexOf(REF), 0, NEW))

Replace items[edit | edit source]

list.splice(2, 1, X)    // list == [a,b,X,d,e]

Removing items[edit | edit source]

list.pop()              // → e    list == [a,b,c,d]
list.shift()            // → a    list == [b,c,d,e]
list.splice(2, 1)       // → [c]  list == [a,b,d,e]

Iterables[edit | edit source]

.filter(n => ...) => array
.find(n => ...)  // es6
.findIndex(...)  // es6
.every(n => ...) => Boolean // ie9+
.some(n => ..) => Boolean   // ie9+
.map(n => ...)   // ie9+
.reduce((total, n) => total) // ie9+
.reduceRight(...)

Credit:rstacruz