I want to iterate over a structure, push chosen nodes to an array and return all of them.

```
var structure = {
    folder: getFolder(1, 'name1'),
    children: [
        {
            folder: getFolder(2, 'name2'),
            children: [
                {
                    folder: getFolder(4, 'name2'),
                    children: []
                }
            ]
        },
        {
            folder: getFolder(3, 'name3'),
            children: []
        }
    ]
};

```

So for example, if folder node matches getFolder(x, 'name2'), I would get an array of two elements:

```
folder: getFolder(2, 'name2'),
children: [
    {
        folder: getFolder(4, 'name2'),
        children: []
    }
]

```

and

```
folder: getFolder(4, 'name2'),
children: []

```

Because both match the given criteria. The function I came up with is:

```
var searchAll = function (data, searchFor, results) {
    results = results || [];
    if (data[searchFor.type] != undefined &&
        data[searchFor.type][searchFor.index].indexOf(searchFor.value) !== -1) {
        return data;
    }
    if (data.children != null) {
        var result = null;
        for (var i = 0; result == null && i < data.children.length; i++) {
            results.push(searchAll(data.children[i], searchFor, results));
        }
    }
    return results;
};

searchAll(structure, {
    type: 'folder',
    index: 'name',
    value: 'name2'
});

```

But it returns undefined. How should I do this?