An open API service indexing awesome lists of open source software.

https://github.com/chans-open-source/any-foreach

可迭代任何内容
https://github.com/chans-open-source/any-foreach

Last synced: 5 months ago
JSON representation

可迭代任何内容

Awesome Lists containing this project

README

          

# any-foreach
> Iterate over anything.Can be break anywhere.

> 可迭代任何内容,可在任意地方中断。

## Install
```cmd
// npm
npm init
npm i any-foreach

// or yarn
yarn init
yarn add any-foreach
```

## Import
```js
const AnyFor = require('any-foreach')
// or
import AnyFor from 'any-foreach'
```
## Iterate
### Array
```js
AnyFor([-1, 0, 1, 2]).for((item, key, originObject) => {
// TODO
// -1, 0, [-1, 0, 1, 2]
// 0, 1, [-1, 0, 1, 2]
// ...
})
```

### Object
```js
AnyFor({ 0: 1, 1: 2, 2: 3 }).for((item, key, originObject) => {
// TODO
// 1, 0, { 0: 1, 1: 2, 2: 3 }
// 2, 1, { 0: 1, 1: 2, 2: 3 }
// 3, 2, { 0: 1, 1: 2, 2: 3 }
})
```

### NodeList
```js
const elementList = document.querySelectorAll('elements')
AnyFor(elementList).for((item, key, originObject) => {
// TODO
// elementItem, elementIndex, elementList
})
```

## Iterate by key
```js
const object = {
a: {
k: 1
},
b: {
k: 2
},
c: {
k: 3
}
}

AnyFor(object).byKey('k', (item, key, originObject, originItem) => {
// TODO
// 1 'a' { a: { k: 1 }, b: { k: 2 }, c: { k: 3 } } { k: 1 }
// 2 'b' { a: { k: 1 }, b: { k: 2 }, c: { k: 3 } } { k: 2 }
// 3 'c' { a: { k: 1 }, b: { k: 2 }, c: { k: 3 } } { k: 3 }
})
```

## Break
> Return true will break the iterate.
```js
AnyFor([-1, 0, 1, 2]).for((item, key, originObject) => {
// TODO
// Break when item is 1
if(item === 1){
return true
}
// -1, 0, [-1, 0, 1, 2]
// 0, 1, [-1, 0, 1, 2]
// 1, 2, [-1, 0, 1, 2]
})
```