Fazila
Fazila

Reputation: 65

Recursive functions for nested objects in javascript

I know this is some really badly written code so any help would be appreciated. I need to return the values of an object and apply the if statements to any nested objects - is it possible to do this recursively rather than repeating the function?

The code is:

function fusion(x, y) {
    var result = {};

    //y = ifMissingInY(x, y)

    Object.keys(x).forEach(key => {
        //console.log(y.hasOwnProperty(key))
        if (y.hasOwnProperty(key) == true) {
            if (x[key] instanceof Array && y[key] instanceof Array) {
                result[key] = x[key].concat(y[key]);
            } else if (typeof (x[key]) == 'number' && typeof (y[key]) == 'number') {
                result[key] = x[key] + y[key]
            } else if (typeof (x[key]) == 'string' && typeof (y[key]) == 'string') {
                result[key] = x[key] + ' ' + y[key]
            } else if (typeof (x[key]) == 'object' && typeof (y[key]) == 'object') {
                Object.keys(x[key]).forEach(key => {
                    fusion.apply(x[key], y[key])
                });


               } else if (typeof (x[key]) !== typeof (y[key])) {
                result[key] = y[key]
            }
        } else {
            result[key] = x[key]
        }
    });

    Object.keys(y).forEach(key => {
        if (x.hasOwnProperty(key) == false) {
            result[key] = y[key]
        }

    })

    return result;
}

I'm testing against this:

console.log(fusion(
    { a: { b: [1, 2], c: { d: 2 } } },
    { a: { b: [0, 2, 1], c: { d: 23 } } }
))

And it needs to return:

{ a: { b: [1, 2, 0, 2, 1], c: { d: 25 } } }

Any help would be much appreciated.

Thanks

Upvotes: 1

Views: 464

Answers (1)

IT goldman
IT goldman

Reputation: 19485

You almost got it. Check out the recursion part, it makes sense (once it's written)

function fusion(x, y) {
    var result = {};

    //y = ifMissingInY(x, y)

    Object.keys(x).forEach(key => {
        //console.log(y.hasOwnProperty(key))
        if (y.hasOwnProperty(key) == true) {
            if (x[key] instanceof Array && y[key] instanceof Array) {
                result[key] = x[key].concat(y[key]);
            } else if (typeof (x[key]) == 'number' && typeof (y[key]) == 'number') {
                result[key] = x[key] + y[key]
            } else if (typeof (x[key]) == 'string' && typeof (y[key]) == 'string') {
                result[key] = x[key] + ' ' + y[key]
            } else if (typeof (x[key]) == 'object' && typeof (y[key]) == 'object') {
                
                    result[key] = fusion(x[key], y[key])
                


               } else if (typeof (x[key]) !== typeof (y[key])) {
                result[key] = y[key]
            }
        } else {
            result[key] = x[key]
        }
    });

    Object.keys(y).forEach(key => {
        if (x.hasOwnProperty(key) == false) {
            result[key] = y[key]
        }

    })

    return result;
}

console.log(fusion(
    { a: { b: [1, 2], c: { d: 2 } } },
    { a: { b: [0, 2, 1], c: { d: 23 } } }
))

Upvotes: 1

Related Questions