更新嵌套对象中的值

时间:2020-08-20 16:03:26

标签: javascript arrays

我具有带有详细信息对象的基本配方数组:

我希望能够更新详细信息中特定项目的值,即ID为5f3aadd5d756e9341ef74e2b的项目

{
  "brandName": "Piatto",
  "_id": "5f3aadd5d756e9341ef74e29",
  "name": "Test",
  "rate": 89,
  "baseQuantity": 1000,
  "baseUnit": "gm",
  "details": [
    {
      "_id": "5f3aadd5d756e9341ef74e2b",
      "rawMaterial": "Egg white 1",
      "brandName": "Bisleri",
      "supplier": "Dasnya",
      "type": "Solid",
      "baseQuantity": 100,
      "baseUnit": "gm",
      "rate": 12,
      "quantityInRecipe": 0,
      "costOfRawMaterial": 0,
      "recipeUnit": "ml",
      "displayRateUnit": "1/2 lit."
    },
    {
      "_id": "5f3aadd5d756e9341ef74e2a",
      "rawMaterial": "Egg white 2",
      "brandName": "Bisleri",
      "supplier": "Dasnya",
      "type": "Solid",
      "baseQuantity": 100,
      "baseUnit": "gm",
      "rate": 14,
      "quantityInRecipe": 0,
      "costOfRawMaterial": 0,
      "recipeUnit": "gm",
      "displayRateUnit": "1/2 lit."
    }
  ],
  "__v": 0
}

请帮助!

2 个答案:

答案 0 :(得分:0)

尝试一下。

var data = {
  "brandName": "Piatto",
  "_id": "5f3aadd5d756e9341ef74e29",
  "name": "Test",
  "rate": 89,
  "baseQuantity": 1000,
  "baseUnit": "gm",
  "details": [{
      "_id": "5f3aadd5d756e9341ef74e2b",
      "rawMaterial": "Egg white 1",
      "brandName": "Bisleri",
      "supplier": "Dasnya",
      "type": "Solid",
      "baseQuantity": 100,
      "baseUnit": "gm",
      "rate": 12,
      "quantityInRecipe": 0,
      "costOfRawMaterial": 0,
      "recipeUnit": "ml",
      "displayRateUnit": "1/2 lit."
    },
    {
      "_id": "5f3aadd5d756e9341ef74e2a",
      "rawMaterial": "Egg white 2",
      "brandName": "Bisleri",
      "supplier": "Dasnya",
      "type": "Solid",
      "baseQuantity": 100,
      "baseUnit": "gm",
      "rate": 14,
      "quantityInRecipe": 0,
      "costOfRawMaterial": 0,
      "recipeUnit": "gm",
      "displayRateUnit": "1/2 lit."
    }
  ],
  "__v": 0
};
var idToBeUpdate = '5f3aadd5d756e9341ef74e2b'
data.details.forEach(x => {
  if (x._id === idToBeUpdate) {
    x.rawMaterial = "Egg white 3 <updated>"
  }
});

console.log(data);

答案 1 :(得分:0)

Ciao,您可以使用filter函数基于_id获取要更新的元素,然后像这样修改它:

let input = {
  "brandName": "Piatto",
  "_id": "5f3aadd5d756e9341ef74e29",
  "name": "Test",
  "rate": 89,
  "baseQuantity": 1000,
  "baseUnit": "gm",
  "details": [
    {
      "_id": "5f3aadd5d756e9341ef74e2b",
      "rawMaterial": "Egg white 1",
      "brandName": "Bisleri",
      "supplier": "Dasnya",
      "type": "Solid",
      "baseQuantity": 100,
      "baseUnit": "gm",
      "rate": 12,
      "quantityInRecipe": 0,
      "costOfRawMaterial": 0,
      "recipeUnit": "ml",
      "displayRateUnit": "1/2 lit."
    },
    {
      "_id": "5f3aadd5d756e9341ef74e2a",
      "rawMaterial": "Egg white 2",
      "brandName": "Bisleri",
      "supplier": "Dasnya",
      "type": "Solid",
      "baseQuantity": 100,
      "baseUnit": "gm",
      "rate": 14,
      "quantityInRecipe": 0,
      "costOfRawMaterial": 0,
      "recipeUnit": "gm",
      "displayRateUnit": "1/2 lit."
    }
  ],
  "__v": 0
}


let elementToUpdate = input.details.filter(el => el._id === "5f3aadd5d756e9341ef74e2b")[0]
// this is the element filtered by _id
console.log(elementToUpdate)
//lets say you want to update rawMaterial
elementToUpdate.rawMaterial += ", Egg black 2"
console.log(elementToUpdate)
// this is the input object modified
console.log(input)