Home > Enterprise >  How to clone only simple property from an object to another?
How to clone only simple property from an object to another?

Time:09-29

I have a object like this:

const obj = {
     "id": 5,
      "adress": "Test"
      "name": "Q1",
      "sections": [
            {
              "code": "S1",
              "label": "LS1",
              "subSections": [  //I can have this level, or not
                {
                  "code": "S1-1",
                  "label": 'LS1-1',
                  "questions": [ // If not, I will have this level instend
                    {
                      "code": "Q1",
                      "label": "LQ1",
                      "answer": [
                        {
                          "code": "A1",
                          "label": 'LA1',
                        }
                      ],
                    }
                  ]
                }
              ],
            }
      ]
}

I want to create a new obj but only with some simple propertys, without the array:

newObj = {
   "id": 5,
    "adress": "Test"
    "name": "Q1",
}

the challenge is to create the new object but in a dynamic form, so if a the first object change with a new property called Date, the new object has to be:

   const newObj = {
       "id": 5,
        "adress": "Test",
        "date": "somedate",
        "name": "Q1",
    }

CodePudding user response:

You can use the spread operator, which will copy the whole object, and use the delete operator to remove part of the object you wish to remove.

const obj = {
"id": 5,
"adress": "Test",
"name": "Q1",
"sections": [
    {
        "code": "S1",
        "label": "LS1",
        "subSections": [  //I can have this level, or not
            {
                "code": "S1-1",
                "label": 'LS1-1',
                "questions": [ // If not, I will have this level instend
                    {
                        "code": "Q1",
                        "label": "LQ1",
                        "answer": [
                            {
                                "code": "A1",
                                "label": 'LA1',
                            }
                        ],
                    }
                ]
            }
             ],
        }
        ]
        }

        const newObj = {...obj};

        delete newObj.sections;

        console.log(newObj); // {id: 5, address: 'Test', name: 'Q1'}

CodePudding user response:

You could destructure the object like so and omit the property you do not want. For example:

const { sections, ...newObj } = obj;

This is by far the simplest method. It will create a variable called sections and put all the other properties into newObj however it's not usually considered good practice to create unused variables like that.

If you do not mind undefined values in your object for the sections property you could alternatively use Object.assign():

const newObj = Object.assign({}, obj, {
    sections: undefined
})

The final option would be to loop over the properties:

const newObj = {};
for (const key of Object.keys(obj)) {
    if (key === 'sections') {
       continue;
    }

    newObj[key] = obj[key];
}

Do note, in all 3 of these examples you are only making a shallow copy of the object. Meaning, while the newObj is a new object the values inside if they are not primitive types (eg. string, number, boolean etc) they will refer to the same instance inside the obj object.

  • Related