physicsboy
physicsboy

Reputation: 6348

How to fix my recursive function? I am receiving either an array of an array of data

I am trying to create a recursive function that will go through an object similar to a directory with subdirectories, and output the'file' objects in an array. However, it seems that i am getting an array of arrays rather than a simple array with the objects I am expecting to see...

The bottom of the code has some console.logs that return:

console.log(findEntry(repAll, '/first')); // ===> [ { name: '/first' }, [] ]
console.log(findEntry(repAll, '/second')); // ===> [ [ { name: '/second' }, { name: '/second' } ] ]

const repAll = { 
    file1: { 
        name: "/first"
    },
    SubDir: { 
        file2: { 
            name: "/second"
        },
        file3: {
            name: "/second"
        }
    } 
};
const req = {};

function findEntry(data, name) {
  let x = [];
    for (const value of Object.values(data)) {
        // Is this a leaf node or a container?
        if (value.name) {
            // Leaf, return it if it's a match
            if (value.name === name) {
                x.push(value);
            }
        } else {
            // Container, look inside it recursively
            const entry = findEntry(value, name);
            x.push(entry);
        }
    }
    return x;
}

console.log('search: /first');
console.log(findEntry(repAll, '/first'));

console.log('search: /second');
console.log(findEntry(repAll, '/second'));

Upvotes: 1

Views: 37

Answers (2)

nAviD
nAviD

Reputation: 3281

With your approach :

function findEntry(data, name,x) {

    for (const value of Object.values(data)) {
        // Is this a leaf node or a container?
        if (value.name) {
            // Leaf, return it if it's a match
            if (value.name === name) {
                x.push(value);
            }
        } else {
            // Container, look inside it recursively
            const entry = findEntry(value, name,x);
            x.push(entry);
        }
    }
    return x;
}

Now call it like this :

let arr=[];
console.log(findEntry(repAll, '/first',arr));

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386816

You could spread the result of findEntry instead of simply pushing the array.

const repAll = { 
    file1: { 
        name: "/first"
    },
    SubDir: { 
        file2: { 
            name: "/second"
        },
        file3: {
            name: "/second"
        }
    } 
};
const req = {};

function findEntry(data, name) {
    let x = [];
    for (const value of Object.values(data)) {
        // Is this a leaf node or a container?
        if (value.name) {
            // Leaf, return it if it's a match
            if (value.name === name) {
                x.push(value);
            }
        } else {
            // Container, look inside it recursively
            x.push(...findEntry(value, name));
        }
    }
    return x;
}

console.log('search: /first');
console.log(findEntry(repAll, '/first'));

console.log('search: /second');
console.log(findEntry(repAll, '/second'));

Upvotes: 2

Related Questions