Skip to content

jonschlinkert/array-each

Repository files navigation

array-each NPM version

Loop over each item in an array and call the given function on every element.

Install with npm

npm i array-each --save

Usage

var each = require('array-each'); var result = []; each(['a', 'b', 'c'], function (ele) { result.push(ele + ele); }); console.log(result); //=> ['aa', 'bb', 'cc']

Return false to "break" early:

var result = []; each(['a', 'b', 'c'], function (ele) { if (ele === 'b') return false; result.push(ele + ele); }); console.log(result); //=> ['aa']

API

Loop over each item in an array and call the given function on every element.

Params

  • array {Array}
  • fn {Function}
  • thisArg {Object}: Optionally pass a thisArg to be used as the context in which to call the function.
  • returns {Array}

Example

each(['a', 'b', 'c'], function (ele) { return ele + ele; }); //=> ['aa', 'bb', 'cc'] each(['a', 'b', 'c'], function (ele, i) { return i + ele; }); //=> ['0a', '1b', '2c']

Related projects

  • arr-filter: Faster alternative to javascript's native filter method.
  • arr-diff: Returns an array with only the unique values from the first array, by excluding all… more
  • arr-map: Faster, node.js focused alternative to JavaScript's native array map.
  • arr-flatten: Recursively flatten an array or arrays. This is the fastest implementation of array flatten.
  • array-unique: Return an array free of duplicate values. Fastest ES5 implementation.
  • array-intersection: Return an array with the unique values present in all given arrays using strict equality… more

Running tests

Install dev dependencies:

npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright (c) 2015 Jon Schlinkert Released under the MIT license.


This file was generated by verb-cli on April 28, 2015.

About

Loop over each item in an array and call the given function on every element.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published