1

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

2
  • So your code IS working, but you would like to change it? I wonder if codereview.stackexchange.com would be the better site for it Commented Jun 22, 2022 at 22:16
  • No the code works for strings, array, numbers but not for object it is returning b: [1, 2, 0, 2, 1] rather than {a: {b: ...} and the only way I can make it work is by repeating the function again in the if object which is really messy. Commented Jun 22, 2022 at 22:19

1 Answer 1

1

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 } } }
))

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much!! I've been trying to figure it out for the past few hours and couldn't get it to work really appreciate this!
You're welcome. I was lucky to nail it first time, but I did write some recursions in my life. like function extend(obj_a, obj_b) which is the same idea.
I understand the concept of recursion but writing the code for it is a whole other matter. Thanks again for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.