2024-11-20 12:56:32 +01:00

53 lines
1.6 KiB
Markdown

---
layout: default
---
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
<h1 id="function-foreach">Function forEach <a href="#function-foreach" title="Permalink">#</a></h1>
Iterate over all elements of a matrix/array, and executes the given callback function.
The callback is invoked with three arguments: the current value,
the current index, and the matrix operated upon.
Note that because the matrix/array might be
multidimensional, the "index" argument is always an array of numbers giving
the index in each dimension. This is true even for vectors: the "index"
argument is an array of length 1, rather than simply a number.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.forEach(x, callback)
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`x` | Matrix &#124; Array | The matrix to iterate on.
`callback` | Function | The callback function is invoked with three parameters: the value of the element, the index of the element, and the Matrix/array being traversed.
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
Type | Description
---- | -----------
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
```js
math.forEach([1, 2, 3], function(value) {
console.log(value)
})
// outputs 1, 2, 3
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[filter](filter.html),
[map](map.html),
[sort](sort.html)