A partial to provide a better foreach capability similar to PHP's foreach construct by differentiating whether the callback function expects a value or a pair of key-value.
npm install js-partial-foreach[![Recent Version][npm-badge]][npm-url]
[![Travis CI - Build Status][travis-badge]][travis-url]
[![Coveralls - Code Coverage Status][cov-badge]][cov-url]
[![David - Dependencies][dep-badge]][dep-url]
[![David - DevDependencies][dev-dep-badge]][dev-dep-url]
[![Doclets][doclets-badge]][doclets-url]
[![Gitter - Repository Chat][chat-badge]][chat-url]
A [partial][partial-link] to provide a better foreach capability in [UMD][umd-link].
This foreach partial automagically detects whether the callback function expects one or multiple arguments
similar to PHP's foreach construct
by value or key-value distinction.
Works with compressed JavaScript code too, also compatible with ECMAScript 6 arrow functions.
```
npm install js-partial-foreach
First, initialize/include the partial, then use it.
The actual usage of the partial is the same across all environment.
- AMD (e.g.: RequireJS)
`javascript`
define(['js-partial-foreach'], function(foreach) {
// you can now use foreach
});
`
- CommonJS (e.g.: NodeJS)
javascript`
var foreach = require('js-partial-foreach');
// you can now use foreach
`
- Browser
javascript`
// load the source from "node_modules/js-partial-foreach/dist/js-partial-foreach.js" - for development
// or from "node_modules/js-partial-foreach/dist/js-partial-foreach.min.js" - for production
var foreach = js_partial_foreach; // it is available in the global namespace
// you can now use foreach
- With arrays
`javascript`
var array = [1, 2, 3, 4, 5];
foreach(
array,
function(value) {
console.log(value); // 1 .. 5
}
);
foreach(
array,
function(key, value) {
console.log(key + ':' + value); // 0:1 .. 4:5
}
);
`
- With objects
javascript`
// for objects
var object = {
a : 1,
b : 2,
c : 3,
d : 4,
e : 5
};
foreach(
object,
function(value) {
console.log(value); // 1 .. 5
}
);
foreach(
object,
function(key, value) {
console.log(key + ':' + value); // a:1 .. e:5
}
);
- With ES6 arrow functions
`javascript`
var array = [1, 2, 3, 4, 5];
foreach(
array,
(value) => {
console.log(value); // 1 .. 5
}
);
foreach(
array,
(key, value) => {
console.log(key + ':' + value); // 0:1 .. 4:5
}
);
- With strings
`javascript``
var string = 'abcdefgh';
foreach(
string,
function(char) {
console.log(char); // 'a' .. 'h'
}
);
Check the source
here
since it's very well documented. Also you can find the rendered jsDoc documentation on
Doclets.io.
Have fun! ;)
If you find any bugs and other issues, check the
GSDC Guide - Issues
section on how to submit issues in a standardized way on
the project's issues page.
In case you have any suggestions regarding the project (features, additional capabilities, etc.), check the
GSDC Guide - Suggestions
section on how to submit suggestions in an easy, standardized way on
the project's issues page.
In order to contribute to this project, check the
GSDC Guide
for an easy, standardized way on how to contribute to projects.
If you by any means find this project useful,
consider supporting the organization.
There are multiple options to support the project and the developers.
Any means of support is beneficial and helpful.
MIT @ Richard King
[npm-badge]: https://img.shields.io/npm/v/js-partial-foreach.svg
[npm-url]: https://www.npmjs.com/package/js-partial-foreach
[travis-badge]: https://travis-ci.org/jsopenstd/js-partial-foreach.svg?branch=master
[travis-url]: https://travis-ci.org/jsopenstd/js-partial-foreach
[cov-badge]: https://coveralls.io/repos/github/jsopenstd/js-partial-foreach/badge.svg?branch=master
[cov-url]: https://coveralls.io/github/jsopenstd/js-partial-foreach
[dep-badge]: https://david-dm.org/jsopenstd/js-partial-foreach.svg
[dep-url]: https://david-dm.org/jsopenstd/js-partial-foreach
[dev-dep-badge]: https://david-dm.org/jsopenstd/js-partial-foreach/dev-status.svg
[dev-dep-url]: https://david-dm.org/jsopenstd/js-partial-foreach#info=devDependencies
[doclets-badge]: https://img.shields.io/badge/style-on_doclets-brightgreen.svg?style=flat-square&label=docs
[doclets-url]: https://doclets.io/jsopenstd/js-partial-foreach/master
[chat-badge]: https://badges.gitter.im/jsopenstd/js-partial-foreach.svg
[chat-url]: https://gitter.im/jsopenstd/js-partial-foreach?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
[partial-link]: https://github.com/jsopenstd/jsopenstd/blob/master/readme.md#partial
[umd-link]: https://github.com/jsopenstd/jsopenstd/blob/master/readme.md#umd