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

3

我卡在一个问题上,试图改变嵌套对象数组中 特定 属性的值:

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个回答

3
为了递归地实现此操作并且不依赖于父键,您可以尝试使用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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接