0

I am workng with react js state i have a fake array in my state name jObj i am copying it using spread operator into variable copy and after that updating jObj using function expandArray don't know why copy variable updating here is my both functions

 fakeMethod=()=>{
        let  jObj=[];
        jObj[0]=[];
        jObj[0]['id']=1;
        jObj[0]["title"]="parent Object";
        jObj[0]['parentId']=[];
        jObj[0]['options']=[];
        jObj[0]['options'][0]=[];
        jObj[0]['options'][0]['name']="Yes";
        jObj[0]['options'][0]['value']="yes";
        jObj[0]['options'][0]['id']=3;
        jObj[0]['options'][1]=[];
        jObj[0]['options'][1]['name']="No";
        jObj[0]['options'][1]['value']="no";
        jObj[0]['options'][1]['id']=4;
        jObj[1]=[];
        jObj[1]['id']=1;
        jObj[1]["title"]="child Object";
        jObj[1]['parentId']=[3,4];
        jObj[1]['options']=[];

        const copy=[...jObj]
        jObj=this.expandArray(jObj);
        console.log("This is orignal object")
        console.log(jObj);
        console.log("This is copy object")
        console.log(copy)
        return ""
    }
    expandArray=(input)=>{
        let result = input.map(a => a.id);
        var max_of_array = Math.max.apply(Math, result);
        var newArr = [];
        for (var i in input) {
            var first = true;
            let objCopy = Object.assign({}, input[i].parentId);
            input[i]=Object.assign(input[i],{"initObjectPos":i});
            if(input[i].parentId.length == 0){
                input[i].parentId="";
            }
            for (var x in objCopy) {
                var y = input[i];

                if (!first) {
                    y = Object.assign({}, y);
                    max_of_array++;
                    y.id = max_of_array.toString();
                    newArr.push(y);
                }
                y.parentId = objCopy[x];
                first = false;
            }
        }
        var output = input.concat(newArr);
        return output;
    }

please help me about where i am wrong i think i am copying array correctly and something is wrong inside expand array function Thanks Much!!

3 Answers 3

2

The spread operator will not create a deep clone of your Object. Since some of your object's properties are objects themselves, their copy will be passed by reference. If you will change one of their attributes, your first Object will be changed again. The easiest way to avoid this is to you use lodash's cloneDeep method. Lodash Documentation

const copy=cloneDeep(jObj)
Sign up to request clarification or add additional context in comments.

Comments

1

the problem is that you're creating an empty array, not an array of objects in the fakeMethod(). if you look into the following code, you'll find the mistake while creating jObj[0], jObj[1], jObj[0]['options'][0];

    let  jObj=[];
    jObj[0]={};
    jObj[0]['id']=1;
    jObj[0]["title"]="parent Object";
    jObj[0]['parentId']=[];
    jObj[0]['options']=[];
    jObj[0]['options'][0]={};
    jObj[0]['options'][0]['name']="Yes";
    jObj[0]['options'][0]['value']="yes";
    jObj[0]['options'][0]['id']=3;
    jObj[0]['options'][1]={};
    jObj[0]['options'][1]['name']="No";
    jObj[0]['options'][1]['value']="no";
    jObj[0]['options'][1]['id']=4;
    jObj[1]={};
    jObj[1]['id']=1;
    jObj[1]["title"]="child Object";
    jObj[1]['parentId']=[3,4];
    jObj[1]['options']=[];

1 Comment

when using you code getting same result you can put you code in my code and check result
1

As of 2024 probably the best method is to use the native structuredClone() method introduced in ES2019

This will copy your array without any references

So instead of:

const copy=[...jObj]

Use:

const copy=structuredClone(jObj)

More information and a polyfill for older browsers can be on this SO answer

Comments

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.