# object-forof

> forOf object iteration — A 'for in'/'Object.keys forEach' alternative

Latest version **1.5.2** (published 2016-03-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install object-forof
pnpm add object-forof
yarn add object-forof
bun add object-forof
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.2 |
| Published | 2016-03-14 |
| First published | 2016-03-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Rob Halff |
| Maintainers | rhalff |

## Links

- npm: https://www.npmjs.com/package/object-forof
- Repository: https://github.com/rhalff/object-forof
- Issues: https://github.com/rhalff/object-forof/issues
- npm.io page: https://npm.io/package/object-forof

## Recent versions

- 1.5.2 (latest) — 2016-03-14
- 1.5.0 — 2016-03-14
- 1.4.1 — 2016-03-14
- 1.4.0 — 2016-03-14
- 1.3.4 — 2016-03-12
- 1.3.3 — 2016-03-12
- 1.3.2 — 2016-03-12
- 1.3.1 — 2016-03-12
- 1.3.0 — 2016-03-10
- 1.2.1 — 2016-03-10
- 1.2.0 — 2016-03-10
- 1.1.0 — 2016-03-10
- 1.0.0 — 2016-03-10

## README

[![NPM](https://nodei.co/npm/object-forof.png)](https://nodei.co/npm/object-forof/)
[![Build Status](https://travis-ci.org/rhalff/object-forof.png)](https://travis-ci.org/rhalff/object-forof)

# Object forOf

Simple object iterator

### Install
```bash
npm install object-forof --save
```

### example

```javascript
const forOf = require('object-forof')

                  // Explanation:
const obj = {
  input: {        // --> type
    in1:          // --> port
      '1',        // --> val
                  // Calls: fn('input', 'in1', '1')

    in2:          // --> port
      '2'         // --> val
                  // Calls: fn('input', 'in1', '2')
  },
  output: {       // --> type
    out1:         // --> port
      '1',        // --> val
                  // Calls: fn('output', 'out1', '1')

    out2:         // --> port
      '2'         // --> val
                  // Calls: fn('output', 'out2', '2')
  }
}

```

Given the above example you could use `Object.keys` like this:
```javascript
Object.keys(obj).forEach((type) => {
  Object.keys(obj[type]).forEach((port) => {
    const val = obj[type][port]
    // do something with type, port, val
  })
})
```

Or a `for in` loop
```javascript
for (let type in obj) {
  if (obj.hasOwnProperty(type)) {
    for (let port in obj[type]) {
      if (obj[type].hasOwnProperty(port)) {
        const val = obj[type][port]
        // do something with type, port, val
      }
    }
  }
}
```

With `forOf`:
```javascript
forOf((type, port, val) => {
  // do something with type, port, val

  // optionally return something
}, obj)
```

If you return something other from the function than `undefined`
it will be added as a value within the returned array.

E.g.
```javascript
forOf((type, port, val) => (
  type === 'input' ? {type: type, val: val} : undefined
), obj)
```

```javascript
forOf((type, port, val) => ({type: type, val: val}), obj)
  .filter((val) => {
  return val.type === 'input'
})
```

Both result in:
```javascript
  [
   {type: 'input', val: '1'},
   {type: 'input', val: '2'}
  ]

```

### Download

  * Development version: https://npmcdn.com/object-forof/dist/forOf.js
  * Minified version: https://npmcdn.com/object-forof/dist/forOf.min.js

---
_Source: https://npm.io/package/object-forof · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
