iterableobject

Turn an object into an iterable object.

Usage no npm install needed!

<script type="module">
  import iterableobject from 'https://cdn.skypack.dev/iterableobject';
</script>

README

iterableobject

Create an iterable from a JavaScript object.

Usage

Normally, you cannot iterate over a JavaScript object. This, for example, will give an error:

var obj = {
    first: {
        name: 'one',
        number: 1
    },
    second: {
        name: 'two',
        number: 2
    }
};

var iterator = obj[Symbol.iterator](); // throws an error

Using iterableobject, you can create an object that can be iterated over and therefore can be used in for...of loops.

var iterable = require('iterableobject');

var armour = {
    helmet: {
        name: 'Helmet of the Gods',
        description: 'An impressive helmet, believed to have come from another realm.'
    },
    boots: {
        name: 'Steel Shoes',
        description: 'A pair of heavy steel shoes. They glisten in the sunlight.'
    }
};

for (var part of iterable(armour)) {
    console.log(part.name + ": " + part.description);
}

The output of the above example will be the following:

Helmet of the Gods: An impressive helmet, believed to have come from another realm.

Steel Shoes: A pair of heavy steel shoes. They glisten in the sunlight.

Development

You can execute the tests for this project by running npm test.

isiterable and object-equal have been used to make testing easier.

license

MIT