I have currently an object which contains a array on which I can iterate.
====== First Step =====
Iterate over an object's attribute instead of the object itself
I am wondering if there's a way to use :
const obj = new YMap();
obj.forEach(...);
instead of
const obj = new YMap();
obj.map.forEach();
EDIT : From VLAZ Comment, I can add this :
this.forEach = function(f) { this.maps.forEach(f);}
In my constructor so my forEach iterate over the maps attribute of my object and not over my object itself.
===== Second Step =====
Avoid forEach imbrication to parse an object attribute of type Array of Map/List/Array
By extends, if I add a little more complexity on my object so it doesn't have an array but an array of list, is there a way to do :
const obj = new YMap();
obj.forEach(...);
instead of
const obj = new YMap();
obj.maps.foreach( function(map) {
map.forEach( function(item) {
...
}
});
To explain it differently, I wonder if there's a way that exist that allow me to have this data structure :
{
maps : [
[ 'A', 'B' ],
[ 'C', 'D' ],
[ 'E', 'F' ]
]
}
Where I could use the code :
obj.forEach(function(item) { console.log(item) });
that would output :
A B C D E F
without having to iterate over the array of array and then iterate on the items of each array using an imbricated forEach
My object looks like the following :
function YMap() {
this.maps = [new HashMap()];
Object.defineProperty(YMap.prototype, 'length', {get: function() {
...
}});
}
YMap.prototype.add = function() {...}
YMap.prototype.get = function() {...}
[...]
module.exports = YMap;