This plugin provides functionality for array iteration with php like methods (next, prev, current, key, end, reset, rewind). And some more functionality.
Add some lines in the head of your HTML document.
<script type="text/javascript" src="mootools.js"></script> <script type="text/javascript" src="Array.Iterator.js"></script>
Create array and iterator( you can create a few instances of it:
var collection = [1,2,3,4,5]; // array var iterator = collection.iterator(options); // iterator var iterator2 = collection.iterator(options); // another iterator
Now you can easily iterate that array:
console.log(iterator.next()); // 1 console.log(iterator.end()); // 5 console.log(iterator.prev()); // 4 console.log(iterator.reset()); // 1 console.log(iterator.slide(3)); // 4 console.log(iterator.slide(-2)); // 2 console.log(iterator.rewind()); // null
You can get array from iterator:
console.log(iterator.ref()); // [1,2,3,4,5] console.log(iterator.ref()===collection) // true
You can get pointer from the iterator:
console.log('value = ' + iterator.jump(2)); // value = 3 console.log('key = ' + iterator.key()); // key = 2
Try to get penultimate value:
iterator.reset(); iterator.jump(-2); console.log(iterator.current()); // 4 iterator.end(); console.log(iterator.prev()); // 4, same thing
Try to play with pit option:
var collection = [1,2,3] var iterator = collection.iterator({pit : true}); console.log(iterator.next()); // 1 console.log(iterator.next()); // 2 console.log(iterator.next()); // 3 // Null index at start(or end), use it to catch end of range. console.log(iterator.next()); // null console.log(iterator.next()); // 1 console.log(iterator.next()); // 2 iterator.options.pit = false; // No more null index when iterating! console.log(iterator.prev()); // 1 console.log(iterator.prev()); // 3
Try to play with limits option:
iterator.options.limits = true; console.log(iterator.jump(0)); // 1 console.log(iterator.next()); // 2 console.log(iterator.next()); // 3 console.log(iterator.next()); // 3 console.log(iterator.next()); // 3
Try to designate boundaries of iterator with min & max options:
iterator.options.min = 1; iterator.options.max = 2; console.log(iterator.reset()); // 2 console.log(iterator.next()); // 3 console.log(iterator.next()); // 3 iterator.options.limits = false; console.log(iterator.next()); // 2
Try to exclude some indexes from iteration with pass option:
var collection = [1,2,3,4,5] var iterator = collection.iterator({pass:2}); console.log(iterator.reset()); // 1 console.log(iterator.next()); // 2 // You skipped one! console.log(iterator.next()); // 4 // Try to skip more. iterator.options.pass = [1,3]; console.log(iterator.reset()); // 1 console.log(iterator.next()); // 3 console.log(iterator.next()); // 5
Try ability to chain method calls(jump, end, reset, rewind, next, prev, slide):
var collection = [1,2,3,4,5] var iterator = collection.iterator({chains:true}); iterator.reset().next().next().current(); // 3 iterator.rewind().slide(-2).current(); // 4
/* create inventory for game */ var inventory = [$('Axe'), $('Potion'), $('MagicBook')]; // Iteration of inventory. var activeSlot = inventory.iterator({ pit : true }); // Try to log all items in inventory do { console.log(activeSlot.next()); } while (activeSlot.valid()); // reset to the null index when yo need it Game.addEvent('inventoryCreate', function(){ activeSlot.rewind(); }); // Try to select last added item of invenroty Game.addEvent('inventoryOpen', function(){ activeSlot.reset(); });
A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.
Found a bug in this plugin? Please report it this repository's Github Issues.
blog comments powered by Disqus