|
| 1 | +# array-each [](http://badge.fury.io/js/array-each) |
| 2 | + |
| 3 | +> Loop over each item in an array and call the given function on every element. |
| 4 | +
|
| 5 | +## Install with [npm](npmjs.org) |
| 6 | + |
| 7 | +```bash |
| 8 | +npm i array-each --save |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```js |
| 14 | +var each = require('array-each'); |
| 15 | + |
| 16 | +var result = []; |
| 17 | +each(['a', 'b', 'c'], function (ele) { |
| 18 | + result.push(ele + ele); |
| 19 | +}); |
| 20 | + |
| 21 | +console.log(result); |
| 22 | +//=> ['aa', 'bb', 'cc'] |
| 23 | +``` |
| 24 | + |
| 25 | +Return `false` to "break" early: |
| 26 | + |
| 27 | +```js |
| 28 | +var result = []; |
| 29 | +each(['a', 'b', 'c'], function (ele) { |
| 30 | + if (ele === 'b') return false; |
| 31 | + result.push(ele + ele); |
| 32 | +}); |
| 33 | + |
| 34 | +console.log(result); |
| 35 | +//=> ['aa'] |
| 36 | +``` |
| 37 | + |
| 38 | +## API |
| 39 | +### [.each](index.js#L34) |
| 40 | + |
| 41 | +Loop over each item in an array and call the given function on every element. |
| 42 | + |
| 43 | +* `array` **{Array}** |
| 44 | +* `fn` **{Function}** |
| 45 | +* `thisArg` **{Object}**: Optionally pass a `thisArg` to be used as the context in which to call the function. |
| 46 | +* `returns`: {Array} |
| 47 | + |
| 48 | +```js |
| 49 | +each(['a', 'b', 'c'], function (ele) { |
| 50 | + return ele + ele; |
| 51 | +}); |
| 52 | +//=> ['aa', 'bb', 'cc'] |
| 53 | + |
| 54 | +each(['a', 'b', 'c'], function (ele, i) { |
| 55 | + return i + ele; |
| 56 | +}); |
| 57 | +//=> ['0a', '1b', '2c'] |
| 58 | +``` |
| 59 | + |
| 60 | +## Related projects |
| 61 | + * [arr-filter](https://github.com/jonschlinkert/arr-filter): Faster alternative to javascript's native filter method. |
| 62 | + * [arr-diff](https://github.com/jonschlinkert/arr-diff): Returns an array with only the unique values from the first array, by excluding all values from additional arrays using strict equality for comparisons. |
| 63 | + * [arr-map](https://github.com/jonschlinkert/arr-map): Faster, node.js focused alternative to JavaScript's native array map. |
| 64 | + * [arr-flatten](https://github.com/jonschlinkert/arr-flatten): Recursively flatten an array or arrays. This is the fastest implementation of array flatten. |
| 65 | + * [array-unique](https://github.com/jonschlinkert/array-unique): Return an array free of duplicate values. Fastest ES5 implementation. |
| 66 | + * [array-intersection](https://github.com/jonschlinkert/array-intersection): Return an array with the unique values present in _all_ given arrays using strict equality for comparisons. |
| 67 | +## Running tests |
| 68 | +Install dev dependencies: |
| 69 | + |
| 70 | +```bash |
| 71 | +npm i -d && npm test |
| 72 | +``` |
| 73 | + |
| 74 | +## Contributing |
| 75 | +Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/array-each/issues) |
| 76 | + |
| 77 | +## Author |
| 78 | + |
| 79 | +**Jon Schlinkert** |
| 80 | + |
| 81 | ++ [github/jonschlinkert](https://github.com/jonschlinkert) |
| 82 | ++ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) |
| 83 | + |
| 84 | +## License |
| 85 | +Copyright (c) 2015 Jon Schlinkert |
| 86 | +Released under the MIT license |
| 87 | + |
| 88 | +*** |
| 89 | + |
| 90 | +_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on April 19, 2015._ |
0 commit comments