Introduction
- MDN Array reference (developer.mozilla.org)
Accessing
list[1] // → b
list.indexOf(b) // → 1
list.lastIndexOf(b) // → 1
list.includes(b) // → true
See: indexOf(), includes() (developer.mozilla.org)
Subsets
Adding items
Inserting
// after -- [_,_,REF,NEW,_,_]
list.splice(list.indexOf(REF) + 1, 0, NEW)
// before -- [_,_,NEW,REF,_,_]
list.splice(list.indexOf(REF), 0, NEW)
See: splice() (developer.mozilla.org)
Replace items
list.splice(2, 1, X) // list == [a,b,X,d,e]
See: splice() (developer.mozilla.org)
Removing items
Iterating
list.forEach(n => ...) // run fn for each item
list.filter(n => ...) // → matching items
list.map(n => ...) // → mapped items
list.find(n => ...) // → first match (ES6)
list.findIndex(n => ...) // → index of first match (ES6)
list.every(n => ...) // → Boolean (ES5)
list.some(n => ...) // → Boolean (ES5)
list.reduce((total, n) => ...) // → single value
0 Comments for this cheatsheet. Write yours!