0

I want to copy some fields in another fields for the same object like in this demo :

var customers = {
    apple: {
        papa: {
            en: "cool" 
        }
    },
    oranges: {
        papa: {
            en: "cool" 
        }
    } 
};

function deepCopyEn(src) {


    if (src.hasOwnProperty("en")) {
      src.fr = src.en;
      src.es = src.en;
    }
    else {
        if (src.constructor === Array) {
            for (var i = 0; i < src.length; i++) {
                deepCopyEn(src[i]);
            }
        }
        else {
            for (var prop in src) {
                if(src.hasOwnProperty(prop)) {
                    deepCopyEn(src[prop]);
                }
            }
        }
    }
}

deepCopyEn(customers);

console.log(customers);

but when I tried with a class with an array and another filed the function don't work, this is an example http://pastebin.com/K7EjAnu1 it gives this error: RangeError: Maximum call stack size exceeded. Any help to update my function??

2
  • Any chance of using Jquery? Commented Mar 24, 2016 at 11:22
  • No, I'm already working with angularJs for the rest of my app Commented Mar 24, 2016 at 11:31

1 Answer 1

1

You could check for scalar types ...

function deepCopyEn(src) {


    if((/string|number|boolean/).test(typeof src)) {
        return;
    }

    if (src.hasOwnProperty("en")) {
    ....
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, this is what I need

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.