0

This is the structure of my data document

{
"_id": "6287a6c5975a25cc25e095b0",
"userName": "Robot",
"projectName": "TestProject",
"projectTypeName": "fixed project",
"profitMargin": 50,
"versions": [
    {
        "ver": 0,
        "data": [
            {
                "totalResources": 2,
                "costToCompany": 31000,
                "profitToCompany": 15500,
                "costToClient": 46500,
                "resources": [
                    {
                        "genericDesignationName": "Development",
                        "designationName": "Backend Developer",
                        "departmentName": "web development",
                        "calculationFactor": 1,
                        "_id": "6287a6c5975a25cc25e095b3"
                    }
                ],
                "addOns": [
                    {
                        "genericDesignationName": "Design",
                        "designationName": "Graphic Designer",
                        "addOnCost": 30000,
                        "_id": "6287a6c5975a25cc25e095b4"
                    }
                ],
                "_id": "6287a6c5975a25cc25e095b2"
            }
        ],
        "_id": "6287a6c5975a25cc25e095b1",
        "createdAt": "2022-05-20T14:33:41.335Z",
        "updatedAt": "2022-05-20T14:33:41.335Z"
    },
    {
        "ver": 1,
        "data": [
            {
                "totalResources": 4,
                "costToCompany": 2200,
                "profitToCompany": 1100,
                "costToClient": 3300,
                "resources": [
                    {
                        "genericDesignationName": "Development",
                        "designationName": "Backend Developer",
                        "departmentName": "web development",
                        "calculationFactor": 1,
                        "_id": "6287a6de975a25cc25e095c2"
                    },
                    {
                        "genericDesignationName": "Development",
                        "designationName": "Frontend Developer",
                        "departmentName": "web development",
                        "calculationFactor": 1,
                        "_id": "6287a6de975a25cc25e095c3"
                    }
                ],
                "addOns": [
                    {
                        "genericDesignationName": "Design",
                        "designationName": "Graphic Designer",
                        "addOnCost": 100,
                        "_id": "6287a6de975a25cc25e095c4"
                    },
                    {
                        "genericDesignationName": "Design",
                        "designationName": "UI/UX Designer",
                        "addOnCost": 100,
                        "_id": "6287a6de975a25cc25e095c5"
                    }
                ],
                "_id": "6287a6de975a25cc25e095c1"
            }
        ],
        "_id": "6287a6de975a25cc25e095c0",
        "createdAt": "2022-05-20T14:34:06.794Z",
        "updatedAt": "2022-05-20T14:34:06.794Z"
    }
],
"createdAt": "2022-05-20T14:33:41.335Z",
"updatedAt": "2022-05-20T14:34:06.795Z",
"__v": 1

}

My question is that How to replace the version[1].data[] with new values that will be come from the client side..

New data will look like

"data": [
            {
                "totalResources": 4,
                "costToCompany": 2200,
                "profitToCompany": 1100,
                "costToClient": 3300,
                "resources": [
                    {
                        "genericDesignationName": "Development",
                        "designationName": "Backend Developer",
                        "departmentName": "web development",
                        "calculationFactor": 1,
                       
                    },
                    {
                        "genericDesignationName": "Development",
                        "designationName": "Frontend Developer",
                        "departmentName": "web development",
                        "calculationFactor": 1,
                        
                    }
                ],
                "addOns": [
                    {
                        "genericDesignationName": "Design",
                        "designationName": "Graphic Designer",
                        "addOnCost": 100,
                       
                    }
                ],
                               }
        ],
        
    }
]
1
  • 3
    Have you tried simply assigning the new value? Like currObj.versions[1].data = newData Commented May 20, 2022 at 17:09

1 Answer 1

1

You can simply achieve that by iterating versions array :

const data = {
  versions: [{
    ver: 0,
    data: []
  }, {
    ver: 1,
    data: []
  }, {
    ver: 2,
    data: []
  }]
};

const replacedData = ['abc'];

data.versions.forEach(obj => {
    if (obj.ver === 1) {
    obj.data = replacedData
  }
});

console.log(data);

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

1 Comment

@GiriSoman Please do accept if it helps. So that it will be helpful for other developers as well.

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.