繁体   English   中英

如何更改嵌套 object 中的值以与数组连接,并将 id 与 javascript 匹配

[英]How to change values in nested object to concat with array, and matching with id with javascript

我有这个 object:

const obj = {
children: [
{
  value: "1",
  label: "Organization",
  Estatus: "1",
  IdTipo: "1",
  children: [
    {
      value: "68",
      label: "REGION 1",
      IdTipo: "3",
      Estatus: "1",
      children: [
        {
          value: "13",
          label: "Store 1",
          IdTipo: "3",
          Estatus: "1",
        },
        {
          value: "15",
          label: "Store 2",
          IdTipo: "3",
          Estatus: "1",
        },
        {
          value: "24",
          label: "Store 3",
          IdTipo: "3",
          Estatus: "1",
        },
      ],
    },
    {
      value: "69",
      label: "REGION 2",
      IdTipo: "3",
      Estatus: "1",
      children: [
        {
          value: "3",
          label: "Store 4",
          IdTipo: "3",
          Estatus: "1",
        },
        {
          value: "11",
          label: "Store 5",
          IdTipo: "3",
          Estatus: "1",
        },
      ],
    },
  ],
},],};

我有这个数组

    const arr = [
{
"id": 13,
"balance": 44958.89
},
{
"id": 15,
"balance": 55687.89
},
{
"id": 24,
"balance": 16768.89
},
]

我需要将 obj 值和 arr id 匹配到 concat label 并在 object 中进行平衡。我期望这样:

{
children: [
{
  value: "1",
  label: "Organization",
  Estatus: "1",
  IdTipo: "1",
  children: [
    {
      value: "68",
      label: "REGION 1",
      IdTipo: "3",
      Estatus: "1",
      children: [
        {
          value: "13",
          label: "Store 1 44958.89",
          IdTipo: "3",
          Estatus: "1",
        },
        {
          value: "15",
          label: "Store 2 55687.89",
          IdTipo: "3",
          Estatus: "1",
        },
        {
          value: "24",
          label: "Store 3 16768.89",
          IdTipo: "3",
          Estatus: "1",
        },
      ],
    },

如您所见,在我的 object 中,标签现在有名称和余额。 我不知道它是否很容易解决,但我很困惑,我不知道如何解决它。 谢谢!!

这是一个具有不可变递归 function 的解决方案:

/**
 * @typedef {object} OrgElement
 * @property {string} value
 * @property {string} label
 * @property {string} IdTipo
 * @property {string} Estatus
 * @property {string} [children]
 */

/**
 *
 * @param {OrgElement[]} orgElements
 * @param {{id: number, balance: number}[]} balanceData
 * @returns {OrgElement[]}
 */
function updateOrgs(orgElements, balanceData) {
  const data = [...balanceData];
  return orgElements.map((orgElement) => {
    const updatedOrgElement = { ...orgElement };
    if (updatedOrgElement.children && data.length > 0) {
      updatedOrgElement.children = updateOrgs(updatedOrgElement.children, data);
    }
    if (updatedOrgElement.value && data.length > 0) {
      const foundItemIndex = data.findIndex(
        (item) => item.id.toString() === updatedOrgElement.value
      );
      if (foundItemIndex > -1) {
        const value = data.splice(foundItemIndex, 1)[0];
        updatedOrgElement.label = `${updatedOrgElement.label} ${value.balance}`;
      }
    }
    return updatedOrgElement;
  });
}

const result = { children: updateOrgs(obj.children, arr) };

暂无
暂无

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

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