can-define
Version:
Create observable objects with JS dot operator compatibility
51 lines (34 loc) • 1.2 kB
Markdown
can-define/map/map.prototype.forEach forEach
can-define/map/map.prototype
Call a function on each property of a DefineMap.
`map.forEach( callback( value, propName ) )`
`forEach` iterates through the map instance, calling a function
for each property value and key.
```js
import {DefineMap} from "can";
const names = [];
const map = new DefineMap({a: "Alice", b: "Bob", e: "Eve"});
map.forEach( (value, propName) => names.push(value) );
console.log( names ); //-> ["Alice", "Bob", "Eve"]
```
{function(*,String)} callback(value,propName) The function to call for each property
The value and key of each property will be passed as the first and second
arguments, respectively, to the callback.
{can-define/map/map} The map instance for chaining.
## Use
If the callback returns `false` the loop will stop.
```js
import {DefineMap} from "can";
const names = [];
const map = new DefineMap({a: "Alice", b: "Bob", e: "Eve"});
map.forEach( (value, propName) => {
if (propName === "e") {
return false;
}
names.push(value);
} );
console.log( names ); //-> ["Alice", "Bob"]
```