js-array: update
This commit is contained in:
parent
aaa6cb7212
commit
36b907944e
105
js-array.md
105
js-array.md
|
@ -1,67 +1,98 @@
|
||||||
---
|
---
|
||||||
title: JavaScript arrays
|
title: JavaScript Arrays
|
||||||
category: JavaScript
|
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
|
```bash
|
||||||
array.indexOf(b) //=> 1
|
list[1] // → b
|
||||||
|
list.indexOf(b) // → 1
|
||||||
|
```
|
||||||
|
|
||||||
### Subsets
|
### Subsets
|
||||||
|
|
||||||
array.slice(0,1) //=> [a]
|
#### Immutable
|
||||||
array.slice(1) //=> [b,c,d,e]
|
|
||||||
array.slice(1,2) //=> [b]
|
|
||||||
|
|
||||||
// Destructive
|
```bash
|
||||||
re = array.splice(1) // re = [b,c,d,e] array == [a]
|
list.slice(0,1) // → [a ]
|
||||||
re = array.splice(1,2) // re = [b,c] array == [a,d,e]
|
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
|
### Adding items
|
||||||
|
|
||||||
array.push(X) // array == [_,_,_,_,_,X]
|
#### Mutative
|
||||||
array.unshift(X) // array == [X,_,_,_,_,_]
|
|
||||||
array.splice(2, 0, X) // array == [_,_,X,_,_,_]
|
|
||||||
|
|
||||||
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
|
### Inserting
|
||||||
|
|
||||||
// after -- [_,_,REF,NEW,_,_]
|
```bash
|
||||||
array.splice(array.indexOf(REF)+1, 0, NEW))
|
// after -- [_,_,REF,NEW,_,_]
|
||||||
|
list.splice(list.indexOf(REF)+1, 0, NEW))
|
||||||
|
```
|
||||||
|
|
||||||
// before -- [_,_,NEW,REF,_,_]
|
```bash
|
||||||
array.splice(array.indexOf(REF), 0, NEW))
|
// before -- [_,_,NEW,REF,_,_]
|
||||||
|
list.splice(list.indexOf(REF), 0, NEW))
|
||||||
|
```
|
||||||
|
|
||||||
### Replace items
|
### 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
|
### Removing items
|
||||||
|
|
||||||
array.pop() //=> e array == [a,b,c,d]
|
```bash
|
||||||
array.shift() //=> a array == [b,c,d,e]
|
list.pop() // → e list == [a,b,c,d]
|
||||||
array.splice(2, 1) //=> [c] array == [a,b,d,e]
|
list.shift() // → a list == [b,c,d,e]
|
||||||
|
list.splice(2, 1) // → [c] list == [a,b,d,e]
|
||||||
|
```
|
||||||
|
|
||||||
### Iterables
|
### Iterables
|
||||||
|
|
||||||
.filter(n => ...) => Array
|
```bash
|
||||||
|
.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(...)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```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