从对象的嵌套数组中更改递归特定属性

时间:2018-10-19 01:21:13

标签: javascript arrays object recursion data-manipulation

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

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对嵌套对象数组的迭代?有任何想法或建议吗?

2 个答案:

答案 0 :(得分:2)

要递归执行此操作而不依赖父键,可以尝试使用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));

答案 1 :(得分:1)

一种替代方法是使用嵌套的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; }