【问题标题】:Change recursively specifics properties from a nested array of objects从嵌套的对象数组中递归更改特定属性
【发布时间】:2019-03-23 21:21:12
【问题描述】:

我在试图更改嵌套对象数组中的特定属性值时遇到问题:

const myObj = [
    {
        "Description":"WA State",
        "Data":[
        {
            "Description":"Years",
            "Indicators":[
            {
                "Year":2018,
                "Points":25994,
                "Goal":"28000",
            }
            ]
        },
        {
            "Description":"Local Goal",
            "Indicators":[
            {
                "Year":2018,
                "Points":25994,
                "Goal":"28000",
            }
            ]
        },
        {
            "Description":"Remote Goal",
            "Indicators":[
            {
                "Year":2018,
                "Points":55857,
                "Goal":"84000",
            }
            ]
        }
        ]
    },

    {
        "Description":"NY State",
        "Data":[
        {
            "Description":"Years",
            "Indicators":[
            {
                "Year":2018,
                "Points":21953,
                "Goal":"26000",
            }
            ]
        },
        {
            "Description":"Local Goal",
            "Indicators":[
            {
                "Year":2018,
                "Points":24195,
                "Goal":"25000",
            }
            ]
        },
        {
            "Description":"Remote Goal",
            "Indicators":[
            {
                "Year":2018,
                "Points":80857,
                "Goal":"90000",
            }
            ]
        }
        ]
    }
]

这里我需要将Year属性的所有外观更改为2017,并将Goal属性的所有外观更改为:50000

我正在考虑有一个对象数组,我可以在其中声明如下内容:

const newValues = [{property: 'Year', newValue: 2019}, {property: 'Goal', newValue: 50000}]

然后用它来比较使用filterreduce对嵌套对象数组的迭代?有什么想法或建议吗?

【问题讨论】:

    标签: javascript arrays object recursion data-manipulation


    【解决方案1】:

    要递归地执行此操作而不依赖于父键,您可以尝试 mapforEach 的组合。

    const myObj = [
      {
        Description: "WA State",
        Data: [
          {
            Description: "Years",
            Indicators: [
              {
                Year: 2018,
                Points: 25994,
                Goal: "28000"
              }
            ]
          },
          {
            Description: "Local Goal",
            Indicators: [
              {
                Year: 2018,
                Points: 25994,
                Goal: "28000"
              }
            ]
          },
          {
            Description: "Remote Goal",
            Indicators: [
              {
                Year: 2018,
                Points: 55857,
                Goal: "84000"
              }
            ]
          }
        ]
      },
    
      {
        Description: "NY State",
        Data: [
          {
            Description: "Years",
            Indicators: [
              {
                Year: 2018,
                Points: 21953,
                Goal: "26000"
              }
            ]
          },
          {
            Description: "Local Goal",
            Indicators: [
              {
                Year: 2018,
                Points: 24195,
                Goal: "25000"
              }
            ]
          },
          {
            Description: "Remote Goal",
            Indicators: [
              {
                Year: 2018,
                Points: 80857,
                Goal: "90000"
              }
            ]
          }
        ]
      }
    ];
    
    function parse(arr) {
      return arr.map(obj => {
        Object.keys(obj).forEach(key => {
          if (Array.isArray(obj[key])) {
            parse(obj[key]);
          }
          
          if (key === 'Year') {
            obj[key] = 2017;
          }
          
          if (key === 'Goal') {
            obj[key] = 50000;
          }
        })
        
        return obj;
      })
    }
    
    console.log(parse(myObj));

    【讨论】:

      【解决方案2】:

      另一种方法是使用嵌套的 forEach 函数和函数 Object.keys 循环遍历键和函数 find 以获取具有要分配的新值的特定对象。

      const myObj = [    {        "Description":"WA State",        "Data":[        {            "Description":"Years",            "Indicators":[            {                "Year":2018,                "Points":25994,                "Goal":"28000",            }            ]        },        {            "Description":"Local Goal",            "Indicators":[            {                "Year":2018,                "Points":25994,                "Goal":"28000",            }            ]        },        {            "Description":"Remote Goal",            "Indicators":[            {                "Year":2018,                "Points":55857,                "Goal":"84000",            }            ]        }        ]    },    {        "Description":"NY State",        "Data":[        {            "Description":"Years",            "Indicators":[            {                "Year":2018,                "Points":21953,                "Goal":"26000",            }            ]        },        {            "Description":"Local Goal",            "Indicators":[            {                "Year":2018,                "Points":24195,                "Goal":"25000",            }            ]        },        {            "Description":"Remote Goal",            "Indicators":[            {                "Year":2018,                "Points":80857,                "Goal":"90000",            }            ]        }        ]    }],
            newValues = [{property: 'Year', newValue: 2019}, {property: 'Goal', newValue: 50000}];
            
      myObj.forEach(o => {
        o.Data.forEach(d => {
          d.Indicators.forEach(i => {
            Object.keys(i).forEach(k => {
              let nv = newValues.find(n => n.property === k);
              if (nv) i[k] = nv.newValue;
            });
          });
        });
      });
      
      console.log(myObj);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        猜你喜欢
        • 2019-03-23
        • 2020-11-03
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 2013-10-11
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        相关资源
        最近更新 更多