简体   繁体   English

如何更新深度嵌套的对象数组?

[英]How to update deeply nested array of objects?

I have the following nested array of objects:我有以下嵌套的对象数组:

const data = [
  {
    product: {
      id: "U2NlbmFyaW9Qcm9swkdWN0OjEsz",
      currentValue: 34300,
    },
    task: {
      id: "R2VuZXJpY1Byb2R1Y3Q6MTA",
      name: "My Annuity",
    },
    instrumentDetails: [
      {
        instrument: {
          id: "U2NlbmFyaW9JbnN0cnVtZW50OjEz",
          supplier: {
            id: "U3VwcGxpZXJJbnN0cnVtZW50OjUzNjQ",
            supplierDetails: {
              name: "Local - Class A",
            },
          },
          currentValue: 44323,
        },
        assets: {
          current: 1.2999270432626702,
          fixed: 0.5144729302004819,
          financial: 0.0723506386331588,
          cash: 0.00006003594786398524,
          alternative: 0.05214078143244779,
          property: 0.548494862567579,
          local: 0.10089348539739094,
          global: 0,
        },
      },
    ],
  },
  {
    product: {
      id: "U2NlbmFyaW9Qcm9swkfefewdWN0OjEsz",
      currentValue: 3435300,
    },
    task: {
      id: "R2VuZXJpYfewfew1Byb2R1Y3Q6MTA",
      name: "Living",
    },
    instrumentDetails: [
      {
        instrument: {
          id: "U2NlbmFyadewwW9JbnN0cnVtZW50OjEz",
          supplier: {
            id: "U3VwcGxpZdwdwXJJbnN0cnVtZW50OjUzNjQ",
            supplierDetails: {
              name: "Local - Class B",
            },
          },
          currentValue: 434323,
        },
        assets: {
          current: 1.294353242,
          fixed: 0.514434242004819,
          financial: 0.07434286331588,
          cash: 0.0000434398524,
          alternative: 0.05242348143244779,
          property: 0.543242567579,
          local: 0.100432439739094,
          global: 0,
        },
      },
    ],
  },
];

The above data presents an array of products which consist of instruments that are described in instrumentDetails array.上述数据显示了一系列产品,这些产品由 instrumentDetails 数组中描述的工具组成。 I am trying to find an instrument by supplier id and update its assets by multiplying all of the asset values by a given number.我正在尝试按供应商 ID 查找工具,并通过将所有资产值乘以给定数字来更新其资产。

Here is my function:这是我的 function:

export const updateObject = (
  productsArr: any,
  supplierInstrumentId: string
) => {
  return productsArr.map(
    (product: any) => {
      product.instrumentDetails.map(
        (instrumentDetail: any) => {
          if (
            instrumentDetail.instrument.supplier.id ===
            supplierInstrumentId
          ) {
            instrumentDetail.assets.current = instrumentDetail.assets.current + 5;
            instrumentDetail.assets.fixed= instrumentDetail.assets.fixed+ 5;
            instrumentDetail.assets.financial= instrumentDetail.assets.financial+ 5;
            instrumentDetail.assets.cash= instrumentDetail.assets.cash+ 5;
          }
        }
      );
    }
  );
};

This function is giving an error:此 function 给出错误:

Uncaught TypeError: Cannot assign to read only property 'current' of object '#'未捕获的类型错误:无法分配给 object '#' 的只读属性“当前”

How can I deeply update the above data?如何深度更新上述数据? Please help.请帮忙。

You need to return a new instrumentDetail-type object from the map function.您需要从 map function 退回一个新的仪器详细类型 object。 Don't try to update the existing object.不要尝试更新现有的 object。

 (instrumentDetail: any) => { const assets = instrumentDetail.instrument.supplier.id === supplierInstrumentId? Object.fromEntries( Object.entries(instrumentDetail.assets).map(([k, v]) => [k, v + 5]) ): instrumentDetail.assets; return {...instrumentDetail, assets }; }

Your product map is not returning which is why you're likely getting an undefined.您的产品 map 没有返回,这就是您可能得到未定义的原因。 I wasn't getting the typescript error which you mentioned above.我没有收到您上面提到的 typescript 错误。 This should leave the array in the state at which you intended.这应该将阵列留在您想要的 state 中。

const updateObject = (
  productsArr: any,
  supplierInstrumentId: string
) => {
  return productsArr.map(
    (product: any) => {
      product.instrumentDetails.map(
        (instrumentDetail: any) => {
          if (
            instrumentDetail.instrument.supplier.id ===
            supplierInstrumentId
          ) {
            instrumentDetail.assets.current += 5;
            instrumentDetail.assets.fixed= instrumentDetail.assets.fixed+ 5;
            instrumentDetail.assets.financial= instrumentDetail.assets.financial+ 5;
            instrumentDetail.assets.cash= instrumentDetail.assets.cash+ 5;
            return instrumentDetail;
          }
        }
      );
      return product;
    }
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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