Skip to content

Commit

Permalink
update(JS): web/javascript/reference/global_objects/array/filter
Browse files Browse the repository at this point in the history
  • Loading branch information
undead404 authored and AdriandeCita committed Oct 9, 2022
1 parent 8f3387e commit 3903b38
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ if (!Array.prototype.filter) {
}
```

Метод `filter()` є [узагальненим](/uk/docs/Web/JavaScript/Reference/Global_Objects/Array#uzahalneni-metody-masyvu). Він лишень очікує, що значення `this` має властивість `length`, а також властивості з цілочисловими ключами.

## Приклади

### Відфільтровування малих значень
Expand Down Expand Up @@ -216,6 +218,29 @@ console.log(filterItems(fruits, "ан")); // ['банан', 'манго']
console.log(filterItems(fruits, "ин")); // ['виноград', 'апельсин']
```

### Використання filter() на розріджених масивах

`filter()` пропустить порожні комірки.

```js
console.log([1, , undefined].filter((x) => x === undefined)); // [undefined]
console.log([1, , undefined].filter((x) => x !== 2)); // [1, undefined]
```

### Виклик filter() на масивоподібних об'єктах

Метод `filter()` зчитує з `this` властивість `length`, а потім звертається до кожного цілочислового індексу.

```js
const arrayLike = {
length: 3,
0: "a",
1: "b",
2: "c",
};
console.log(Array.prototype.filter.call(arrayLike, (x) => x <= "b")); // [ 'a', 'b' ]
```

### Внесення змін до початкового масиву (зміна, додавання і видалення елементів)

Наступний приклад перевіряє поведінку методу `filter` під час внесення змін до масиву.
Expand Down

0 comments on commit 3903b38

Please sign in to comment.