Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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 } } }
))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading