简体   繁体   中英

Replacing a nested array inside an array of objects with new array of objects

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,
                       
                    }
                ],
                               }
        ],
        
    }
]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM