Skip to content

Commit

Permalink
Merge pull request #1393 from ember-learn/kategengler-patch-1
Browse files Browse the repository at this point in the history
Correct uses of filter -- Array.prototype.filter doesn't mutate
  • Loading branch information
jaredgalanis authored Sep 19, 2024
2 parents 4ffb570 + 501ae8d commit 75af194
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions content/ember/v5/deprecate-array-prototype-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ someArray.compact();

After:
```js
someArray.filter(val => val !== undefined && val !== null);
someArray = someArray.filter(val => val !== undefined && val !== null);
```
#### `filterBy`

Expand All @@ -60,8 +60,8 @@ someArray.filterBy('food', 'beans'); // [{ food: 'beans', isFruit: false }]

After:
```js
const someArray = [{ food: 'apple', isFruit: true }, { food: 'beans', isFruit: false }];
someArray.filter(el => el.food === 'beans'); // [{ food: 'beans', isFruit: false }]
let someArray = [{ food: 'apple', isFruit: true }, { food: 'beans', isFruit: false }];
someArray = someArray.filter(el => el.food === 'beans'); // [{ food: 'beans', isFruit: false }]
```
#### `findBy`

Expand Down Expand Up @@ -205,8 +205,8 @@ someArray.reject(el => el.isFruit); // [{ food: 'beans', isFruit: false }]
After:
```js
const someArray = [{ food: 'apple', isFruit: true }, { food: 'beans', isFruit: false }];
someArray.filter(el => !el.isFruit); // [{ food: 'beans', isFruit: false }]
let someArray = [{ food: 'apple', isFruit: true }, { food: 'beans', isFruit: false }];
someArray = someArray.filter(el => !el.isFruit); // [{ food: 'beans', isFruit: false }]
```
#### `rejectBy`
Expand All @@ -218,8 +218,8 @@ someArray.rejectBy('isFruit'); // [{ food: 'beans', isFruit: false }]
After:
```js
const someArray = [{ food: 'apple', isFruit: true }, { food: 'beans', isFruit: false }];
someArray.filter(el => !el.isFruit); // [{ food: 'beans', isFruit: false }]
let someArray = [{ food: 'apple', isFruit: true }, { food: 'beans', isFruit: false }];
someArray = someArray.filter(el => !el.isFruit); // [{ food: 'beans', isFruit: false }]
```
#### `sortBy`
Expand Down Expand Up @@ -301,8 +301,8 @@ someArray.without('a'); // ['b', 'c']
After
```js
const someArray = ['a', 'b', 'c'];
someArray.filter(el => el !== 'a'); // ['b', 'c']
let someArray = ['a', 'b', 'c'];
someArray = someArray.filter(el => el !== 'a'); // ['b', 'c']
```
Please make sure `without` reactivity is fully tested.
Expand Down Expand Up @@ -866,7 +866,10 @@ export default class SampleComponent extends Component {

@action
removeObject(value) {
this.abc.filter(item => item !== value);
const index = this.abc.indexOf(value);
if (index !== -1) {
this.abc.splice(index, 1);
}
}
}
```
Expand Down

0 comments on commit 75af194

Please sign in to comment.