# Improve the array.forEach method with an optional predicate function parameter

**URL:** <https://es.discourse.group/t/improve-the-array-foreach-method-with-an-optional-predicate-function-parameter/571>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [December 6, 2020, 4:14pm UTC](https://es.discourse.group/t/improve-the-array-foreach-method-with-an-optional-predicate-function-parameter/571 "2020-12-06T16:14:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mckenney17](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mckenney17/32/531_2.png) [@Mckenney17](https://es.discourse.group/u/Mckenney17)\
**Post date:** [December 6, 2020, 4:14pm UTC](https://es.discourse.group/t/improve-the-array-foreach-method-with-an-optional-predicate-function-parameter/571/1 "2020-12-06T16:14:34Z")

</div>

This idea came when I was trying to implement a reusable code library. The method I called `forWhich`. So I thought, having it as an improvement on `forEach` would be better.

So here goes the definition:  
_forEach element in this array, execute this **callback function** , (optionally)provided it returns true for this **predicate function** _

- This eases the work of **having to filter() an array for elements you don't want, before performing an action on all of them**.
- Also eases the work of, **having to find() an element in an array before performing an action on it**
- It is a good refactoring code for these task.
- It would be very useful in DOM manipulation, as the `forEach` method is used most in that case(I think).
- **More Importantly** : It's not every case I would want to filter my array. I might want to manipulate one or some of the array elements.
- Consider: Which one would be faster: **having to filter an array first before executing callback** or **checking for condition on an array item before executing callback**
- There are a lot ways to solve a problem, but we aim for speed and less memory

Syntax:

```javascript
array.forEach(callback, [predicate]);

```

Sample code:  
Make each element's background blue provided it has the blue class. _Although this is one that could be done easily in css with a class_.

```javascript
[DOMelem1, DOMelem2, ...]
.forEach((el) => {
// callback func
	makeBgBlue(el);
},
(el) => {
// predicate func
	return el.classlist.contains('make-blue');
});

```

Sample code 2:  
Multiply each obj's **x** property by 2, provided its **id** property is equal to 'someId'

```javascript
[obj1, obj2, ...]
.forEach((obj) => obj.x * 2, (obj) => obj.id === 'someId');

```

I don't know how this would be implemented, **it may require a new Array method**... As I know the TC39 wouldn't wanna break existing code, which in this case the **thisArg** parameter, which would normally be the second parameter to the function.

I was also thinking it could be implemented for the **map** array method also, but I haven't thought that through.

Thanks for considering this proposal.  
_JS ❤ - Kehinde Ogunrinola_

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [December 6, 2020, 5:11pm UTC](https://es.discourse.group/t/improve-the-array-foreach-method-with-an-optional-predicate-function-parameter/571/2 "2020-12-06T17:11:42Z")

</div>

I wonder why you are not just writing

```js
array.filter(predicate).forEach(callback);

```

I'm not sure what you mean by

> [@Mckenney17](#):
>
> This eases the work of **having to filter() an array for elements you don't want**

---

<div class="post-metadata">

**Author:** ![Mckenney17](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mckenney17/32/531_2.png) [@Mckenney17](https://es.discourse.group/u/Mckenney17)\
**Post date:** [December 6, 2020, 6:14pm UTC](https://es.discourse.group/t/improve-the-array-foreach-method-with-an-optional-predicate-function-parameter/571/3 "2020-12-06T18:14:41Z")

</div>

Ok. It's not every case you wanna filter your array is it? You might want to affect just some part of it.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 6, 2020, 7:02pm UTC](https://es.discourse.group/t/improve-the-array-foreach-method-with-an-optional-predicate-function-parameter/571/4 "2020-12-06T19:02:13Z")

</div>

filter doesn’t mutate, so you can ignore the returned array, and nothing will have been removed from the original.
