js-array: update
This commit is contained in:
parent
aaa6cb7212
commit
36b907944e
87
js-array.md
87
js-array.md
|
@ -1,67 +1,98 @@
|
|||
---
|
||||
title: JavaScript arrays
|
||||
title: JavaScript Arrays
|
||||
category: JavaScript
|
||||
layout: 2017/sheet
|
||||
---
|
||||
|
||||
## JavaScript arrays
|
||||
### Arrays
|
||||
|
||||
array = [a,b,c,d,e]
|
||||
```bash
|
||||
list = [a,b,c,d,e]
|
||||
```
|
||||
{: .-setup}
|
||||
|
||||
array[1] //=> b
|
||||
array.indexOf(b) //=> 1
|
||||
```bash
|
||||
list[1] // → b
|
||||
list.indexOf(b) // → 1
|
||||
```
|
||||
|
||||
### Subsets
|
||||
|
||||
array.slice(0,1) //=> [a]
|
||||
array.slice(1) //=> [b,c,d,e]
|
||||
array.slice(1,2) //=> [b]
|
||||
#### Immutable
|
||||
|
||||
// Destructive
|
||||
re = array.splice(1) // re = [b,c,d,e] array == [a]
|
||||
re = array.splice(1,2) // re = [b,c] array == [a,d,e]
|
||||
```bash
|
||||
list.slice(0,1) // → [a ]
|
||||
list.slice(1) // → [ b,c,d,e]
|
||||
list.slice(1,2) // → [ b ]
|
||||
```
|
||||
|
||||
#### Mutative
|
||||
|
||||
```bash
|
||||
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
|
||||
|
||||
array.push(X) // array == [_,_,_,_,_,X]
|
||||
array.unshift(X) // array == [X,_,_,_,_,_]
|
||||
array.splice(2, 0, X) // array == [_,_,X,_,_,_]
|
||||
#### Mutative
|
||||
|
||||
array.concat([X,Y]) //=> [_,_,_,_,_,X,Y]
|
||||
```bash
|
||||
list.push(X) // list == [_,_,_,_,_,X]
|
||||
list.unshift(X) // list == [X,_,_,_,_,_]
|
||||
list.splice(2, 0, X) // list == [_,_,X,_,_,_]
|
||||
```
|
||||
|
||||
#### Immutable
|
||||
|
||||
```bash
|
||||
list.concat([X,Y]) // → [_,_,_,_,_,X,Y]
|
||||
```
|
||||
|
||||
### Inserting
|
||||
|
||||
```bash
|
||||
// after -- [_,_,REF,NEW,_,_]
|
||||
array.splice(array.indexOf(REF)+1, 0, NEW))
|
||||
list.splice(list.indexOf(REF)+1, 0, NEW))
|
||||
```
|
||||
|
||||
```bash
|
||||
// before -- [_,_,NEW,REF,_,_]
|
||||
array.splice(array.indexOf(REF), 0, NEW))
|
||||
list.splice(list.indexOf(REF), 0, NEW))
|
||||
```
|
||||
|
||||
### Replace items
|
||||
|
||||
array.splice(2, 1, X) // array == [a,b,X,d,e]
|
||||
```bash
|
||||
list.splice(2, 1, X) // list == [a,b,X,d,e]
|
||||
```
|
||||
|
||||
### Removing items
|
||||
|
||||
array.pop() //=> e array == [a,b,c,d]
|
||||
array.shift() //=> a array == [b,c,d,e]
|
||||
array.splice(2, 1) //=> [c] array == [a,b,d,e]
|
||||
```bash
|
||||
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
|
||||
|
||||
.filter(n => ...) => Array
|
||||
```bash
|
||||
.filter(n => ...) => array
|
||||
```
|
||||
|
||||
```bash
|
||||
.find(n => ...) // es6
|
||||
.findIndex(...) // es6
|
||||
```
|
||||
|
||||
```bash
|
||||
.every(n => ...) => Boolean // ie9+
|
||||
.some(n => ..) => Boolean // ie9+
|
||||
```
|
||||
|
||||
```bash
|
||||
.map(n => ...) // ie9+
|
||||
.reduce((total, n) => total) // ie9+
|
||||
.reduceRight(...)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue