【问题标题】:Create a new array by pushing json element and add new attribute in same pushed json object通过推送 json 元素创建一个新数组,并在同一个推送的 json 对象中添加新属性
【发布时间】:2022-01-05 19:43:45
【问题描述】:

基本上我有一个 json 数组,我想创建一个新数组,我想在其中推送所有 childNode 并在 ChildNode 中推送其各自的资金来源名称

var data = vardata={
  "costdata": [
    {
      fundingSource: 'A',
      childNode: [
        {
          childName: 'x',
          amt: 100
        }
      ]
    },
    {
      fundingSource: 'B',
      childNode: [
        {
          childName: 'y',
          amt: 200
        }
      ]
    }
  ]
}

预期的输出是创建单个 json 数组并在各自的 childNode 属性中推送资金来源元素

vardata={
  "costdata": [
    {
      childNode: [
        {
          fundingSource: 'A',
          childName: 'x',
          amt: 100
        }
      ]
    },
    {
      childNode: [
        {
          fundingSource: 'B',
          childName: 'y',
          amt: 200
        }
      ]
    }
  ]
}

【问题讨论】:

  • JSON 是一种用于数据交换的文本符号(More here.) 如果您正在处理 JavaScript 源代码,而不是处理 string,那么您就不是在处理 JSON。
  • 你最好的选择是做你的研究,search 寻找关于 SO 和其他地方的相关主题,然后试一试。 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布minimal reproducible example 展示您的尝试并具体说明您遇到的问题。人们会很乐意提供帮助。

标签: node.js json loops


【解决方案1】:
  var data = {
    "costdata": [
      {
        fundingSource: 'A',
        childNode: [
          {
            childName: 'x',
            amt: 100
          }
        ]
      },
      {
        fundingSource: 'B',
        childNode: [
          {
            childName: 'y',
            amt: 200
          }
        ]
      }
    ]
  }

let newData = [];
  for (let i = 0; i < data.costdata.length; i++) {
    const child = data.costdata[i];
    let childAry = [];
    if (child.childNode.length > 0) {
      childAry = [];
      for (let j = 0; j < child.childNode.length; j++) {
        const node = child.childNode[j];
        let dataObj = {
          fundingSource: child.fundingSource,
          childName: node.childName,
          amt: node.amt
        };
        childAry.push(dataObj);
      }
    }
    newData.push({ 'childNode': childAry });
  }
  console.log('newData==>', newData);

这是一个 for 循环来获得您的预期输出

【讨论】:

  • 一个好的答案将始终包括解释为什么这会解决问题,以便 OP 和任何未来的读者可以从中学习。
【解决方案2】:

如果你只想格式化对象,这里的解决方案是:

var data = {
    "costdata": [
        {
            fundingSource: 'A',
            childNode: [
                {
                    childName: 'x',
                    amt: 100
                },
            ]
        },
        {
            fundingSource: 'B',
            childNode: [
                {
                    childName: 'y',
                    amt: 200
                }
            ]
        }
    ]
}

data["costdata"] = data["costdata"].map(el => {
    const fundingSource = el.fundingSource

    const updChildNode = el.childNode.map(child => {
        return {
            fundingSource,
            ...child
        }
    })
    return updChildNode
})

console.log(data)

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多