Skip to content
Alex edited this page May 31, 2015 · 3 revisions

Overview

var iterator = new RecursiveIterator(
    root /*{Object|Array}*/,
    [bypassMode=0] /*{Number}*/,
    [ignoreCircular=false] /*{Boolean}*/,
    [maxDeep=100] /*{Number}*/
);

Tree traverse methods (bypassMode)

By default method bypass of tree is vertical bypassMode=0:

var root = {
    object: {
        number: 1
    },
    string: 'foo'
};

for(let {node, path} of new RecursiveIterator(root)) {
    console.log(path.join('.'), node);
}

// object    Object {number: 1}
// object.number    1
// string    foo

You can change it on horizontal by passing the bypassMode=1:

var root = {
    object: {
        number: 1
    },
    string: 'foo'
};

for(let {node, path} of new RecursiveIterator(root, 1)) {
    console.log(path.join('.'), node);
}

// object    Object {number: 1}
// string    foo
// object.number    1

Circular references (ignoreCircular)

By default, if detected circular reference then will throw an exception:

var root = {
    number: 1,
    object: undefined,
    string: 'foo'
};
root.object = root;

for(let {node, path} of new RecursiveIterator(root)) {
    console.log(path.join('.'), node);
}

// number    1
// Uncaught Error: Circular reference

You can change this behaviour by passing ignoreCircular=true:

var root = {
    number: 1,
    object: undefined,
    string: 'foo'
};
root.object = root;

for(let {node, path} of new RecursiveIterator(root, 0, true)) {
    console.log(path.join('.'), node);
}

// number    1
// string    foo

Max deep (maxDeep)

You can control the depth of dives of the iterator. By default maxDeepis 100. Minimum depth is 1. Each like cycle:

var root = {
    object: {
        number: 1
    },
    string: 'foo'
};

for(let {node, path} of new RecursiveIterator(root, 0, false, 1)) {
    console.log(path.join('.'), node);
}

// object    Object {number: 1}
// string    foo