Skip to content

Commit 3522d28

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

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,113 @@ const arr = [1, 2, 3, 4, 5];
6565
const newArr = arr.slice(1, 4);
6666
// newArr = [2, 3, 4]
6767
```
68+
69+
## Search among elements
70+
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
73+
74+
```js
75+
const arr = [10, 20, 20, 30];
76+
const first = arr.indexOf(20); // first = 1
77+
const last = arr.lastIndexOf(20); // last = 2
78+
```
79+
80+
- includes(value) <br>
81+
returns true if the array has value, else false
82+
83+
```js
84+
const arr = [10, 20, 30, 40];
85+
const n = arr.includes(15); // false
86+
```
87+
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
90+
91+
```js
92+
const arr = [1, 13, 23, 43, 56, 71, 97]
93+
const evenNum = arr.find(item => {
94+
return % 2 === 0
95+
})
96+
// evenNum = 56
97+
```
98+
99+
- filter(func(item, index, arr)) <br>
100+
the method is similar to `find()`, but `filter()` returns an array of all matching elements
101+
102+
```js
103+
const arr = [1, 2, 3, 4, 5, 6, 7];
104+
const evenArr = arr.filter((item) => {
105+
return item % 2 === 0;
106+
}); // evenArr = [2, 4, 6]
107+
```
108+
109+
## Iteration of elements
110+
111+
- forEach(func(item, index, array)) <br>
112+
calls `func()` for every element in array, does not return anything
113+
114+
```js
115+
const arr = [1, 2, 3];
116+
arr.forEach((item, index, arr) => {
117+
console.log(item, index, arr);
118+
});
119+
// 1 0 [1, 2, 3]
120+
// 2 1 [1, 2, 3]
121+
// 3 2 [1, 2, 3]
122+
```
123+
124+
## Transform the array
125+
126+
- map(func(item, index, array)) <br>
127+
calls the `func()` for each element of the array and returns the array of results
128+
129+
```js
130+
const arr = [1, 2, 3, 4, 5];
131+
const newArr = arr.map((item) => {
132+
return item * 10;
133+
});
134+
// newArr = [10, 20, 30, 40, 50]
135+
```
136+
137+
- sort(func()) <br>
138+
sorts the array changing it's element order, `func()` defining the sorting order (optional).
139+
140+
```js
141+
const arr = [25, 7, 13 31]
142+
arr.sort((a, b) => {
143+
return a - b
144+
})
145+
// arr = [7, 13, 25, 31]
146+
```
147+
148+
- reverse() <br>
149+
reverse the order of the elements of the current array
150+
151+
```js
152+
const arr = [1, 2, 3, 4, 5];
153+
arr.reverse();
154+
// arr = [5, 4, 3, 2, 1]
155+
```
156+
157+
- join(separator)/string.split(separator) <br>
158+
convert an array to string / a string to array
159+
160+
```js
161+
const str = "one, two, three, four";
162+
const arr = str.split(", ");
163+
// arr = ["one", "two", "three", "four"]
164+
const newStr = arr.join("-");
165+
// newStr = "one-two-three-four"
166+
```
167+
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
170+
171+
```js
172+
const arr = [1, 2, 3, 4, 5];
173+
const sumFromElements = arr.reduce((sum, current) => {
174+
return sum + current;
175+
}, 0);
176+
// sumFromElements = 15
177+
```

0 commit comments

Comments
 (0)