用新的对象数组替换对象数组中的嵌套数组

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

这是我的数据文档的结构

{
"_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

}

我的问题是如何用来自客户端的新值替换版本[1].data[]..

新数据看起来像

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

您可以简单地通过迭代 versions 数组 :

来实现

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