Skip to content

Commit f98ec1c

Browse files
committed
Update js-array-methods.md
1 parent 3522d28 commit f98ec1c

1 file changed

Lines changed: 34 additions & 34 deletions

File tree

JavaScript/Array methods/js-array-methods.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,44 @@
22

33
## Adding/Removing elements
44

5-
- push(...items) <br>
6-
add elements to the end
5+
- **push**(...items) <br>
6+
_add `items` to the end_
77

88
```js
99
const arr = [1, 2, 3];
1010
arr.push(4);
1111
// arr = [1, 2, 3, 4]
1212
```
1313

14-
- pop() <br>
15-
extracts an element from the end
14+
- **pop**() <br>
15+
_extracts an element from the end_
1616

1717
```js
1818
const arr = [1, 2, 3];
1919
arr.pop();
2020
// arr = [1, 2]
2121
```
2222

23-
- unshift(...items) <br>
24-
add items to the beginning (bad for performance)
23+
- **unshift**(...items) <br>
24+
_add `items` to the beginning (bad for performance)_
2525

2626
```js
2727
const arr = [1, 2, 3];
2828
arr.unshift(0);
2929
// arr = [0, 1, 2, 3]
3030
```
3131

32-
- shift() <br>
33-
extracts from the beginning
32+
- **shift**() <br>
33+
_extracts from the beginning_
3434

3535
```js
3636
const arr = [1, 2, 3];
3737
arr.shift();
3838
// arr = [2, 3]
3939
```
4040

41-
- concat(...items) <br>
42-
returns a new array: copy of the current array + `items`
41+
- **concat**(...items) <br>
42+
_returns a new array: copy of the current array + `items`_
4343

4444
```js
4545
const arr = [1, 2, 3];
@@ -48,17 +48,17 @@ const newArr2 = newArr.concat([6, 7]);
4848
// newArr2 = [1, 2, 3, 4, 5, 6, 7]
4949
```
5050

51-
- splice(pos, count, ...items) <br>
52-
at index `pos` deletes `count` elements and inserts `items`
51+
- **splice**(pos, count, ...items) <br>
52+
_at index `pos` deletes `count` elements and inserts `items`_
5353

5454
```js
5555
const arr = ["a", "*", "*", "d"];
5656
arr.splice(1, 2, "b", "c");
5757
// arr = ['a', 'b', 'c', d']
5858
```
5959

60-
- slice(start, end) <br>
61-
creates a new array, copies elements from index `start` till `end` (not inclusive) into it
60+
- **slice**(start, end) <br>
61+
_creates a new array, copies elements from index `start` till `end` (not inclusive) into it_
6262

6363
```js
6464
const arr = [1, 2, 3, 4, 5];
@@ -68,25 +68,25 @@ const newArr = arr.slice(1, 4);
6868

6969
## Search among elements
7070

71-
- indexOf/lastIndexOf(item, pos) <br>
72-
look for `item` starting from index `pos`, return the index or -1 if not found. Method `lastIndexOf()` searches from right to left
71+
- **indexOf / lastIndexOf**(item, pos) <br>
72+
_look for `item` starting from index `pos`, return it's index or -1 if not found. Method `lastIndexOf()` searches from right to left_
7373

7474
```js
7575
const arr = [10, 20, 20, 30];
7676
const first = arr.indexOf(20); // first = 1
7777
const last = arr.lastIndexOf(20); // last = 2
7878
```
7979

80-
- includes(value) <br>
81-
returns true if the array has value, else false
80+
- **includes**(value) <br>
81+
_returns true if the array has value, else false_
8282

8383
```js
8484
const arr = [10, 20, 30, 40];
8585
const n = arr.includes(15); // false
8686
```
8787

88-
- find/findIndex(func(item, index, arr)) <br>
89-
`func()` is called for each element of the array. If the `func()` returns true, the search is interrupted and item is returned, else - undefined
88+
- **find / findIndex**(func(item, index, arr)) <br>
89+
_`func()` is called for each element of the array. If the `func()` returns true, the search is interrupted and item is returned, else - undefined_
9090

9191
```js
9292
const arr = [1, 13, 23, 43, 56, 71, 97]
@@ -96,8 +96,8 @@ const evenNum = arr.find(item => {
9696
// evenNum = 56
9797
```
9898

99-
- filter(func(item, index, arr)) <br>
100-
the method is similar to `find()`, but `filter()` returns an array of all matching elements
99+
- **filter**(func(item, index, arr)) <br>
100+
_the method is similar to `find()`, but `filter()` returns an array of all matching elements_
101101

102102
```js
103103
const arr = [1, 2, 3, 4, 5, 6, 7];
@@ -108,8 +108,8 @@ const evenArr = arr.filter((item) => {
108108

109109
## Iteration of elements
110110

111-
- forEach(func(item, index, array)) <br>
112-
calls `func()` for every element in array, does not return anything
111+
- **forEach**(func(item, index, array)) <br>
112+
_calls `func()` for every element in array, does not return anything_
113113

114114
```js
115115
const arr = [1, 2, 3];
@@ -123,8 +123,8 @@ arr.forEach((item, index, arr) => {
123123

124124
## Transform the array
125125

126-
- map(func(item, index, array)) <br>
127-
calls the `func()` for each element of the array and returns the array of results
126+
- **map**(func(item, index, array)) <br>
127+
_calls the `func()` for each element of the array and returns the array of results_
128128

129129
```js
130130
const arr = [1, 2, 3, 4, 5];
@@ -134,8 +134,8 @@ const newArr = arr.map((item) => {
134134
// newArr = [10, 20, 30, 40, 50]
135135
```
136136

137-
- sort(func()) <br>
138-
sorts the array changing it's element order, `func()` defining the sorting order (optional).
137+
- **sort**(func()) <br>
138+
_sorts the array changing it's element order, `func()` defining the sorting order (optional)._
139139

140140
```js
141141
const arr = [25, 7, 13 31]
@@ -145,17 +145,17 @@ arr.sort((a, b) => {
145145
// arr = [7, 13, 25, 31]
146146
```
147147

148-
- reverse() <br>
149-
reverse the order of the elements of the current array
148+
- **reverse**() <br>
149+
_reverse the order of the elements of the current array_
150150

151151
```js
152152
const arr = [1, 2, 3, 4, 5];
153153
arr.reverse();
154154
// arr = [5, 4, 3, 2, 1]
155155
```
156156

157-
- join(separator)/string.split(separator) <br>
158-
convert an array to string / a string to array
157+
- **join**(separator) / string.split(separator) <br>
158+
_convert an array to string / a string to array_
159159

160160
```js
161161
const str = "one, two, three, four";
@@ -165,8 +165,8 @@ const newStr = arr.join("-");
165165
// newStr = "one-two-three-four"
166166
```
167167

168-
- reduce/reduceRight(func(accumulator, current, index, array), initial) <br>
169-
calculate a single value over the array by calling `func()` for each element and passing an intermediate result between the calls
168+
- **reduce / reduceRight**(func(accumulator, current, index, array), initial) <br>
169+
_calculate a single value over the array by calling `func()` for each element and passing an intermediate result between the calls_
170170

171171
```js
172172
const arr = [1, 2, 3, 4, 5];

0 commit comments

Comments
 (0)