繁体   English   中英

Javascript 遍历嵌套的 Object 并返回转换并重命名的对象数组

[英]Javascript iterate through nested Object and return transformed and renamed Array of objects

我已经检查了有关嵌套对象的所有可用答案,但到目前为止没有任何帮助。 我确实有一个深层嵌套的 object ,例如:

let datastat = 
{
  "statisticData": {
    "lastStatisticOne": {
      "min": 0,
      "max": 10
    },
    "lastStatisticTwo": {
      "min": 0,
      "max": 20
    },

    "firstStatisticOne": {
      "min": 0,
      "max": 30,
    },
    "firstStatisticTwo": {
      "min": 0,
      "max": 40,
    },
  },
  "statisticValue": [
   none important Data
  ]
}

我试图实现的是:

statisticNew =
[
  {
   "lastStatisticOne": [{ "name": "min", "value": 0 }, { "name": "max", "value": 10 }]
  },
  {
   "lastStatisticTwo": [{ "name": "min", "value": 0 }, { "name": "max", "value": 20 }]
  },
  {
   "firstStatisticOne": [{ "name": "min", "value": 0 }, { "name": "max", "value": 30 }]
  },
  {
   "firstStatisticTwo": [{ "name": "min", "value": 0 }, { "name": "max", "value": 40 }]
  }
]

我的尝试失败了:

const statistic = [];
statistic.push(datastat.statisticData);

for(let item in statistic){
 if (statistic.hasOwnProperty(item)) {
  const result = Object.keys(statistic[0]).map(e => ({name: e, value: statistic[0][e]}));
  console.log(result);
 }
}

如何实现正确的 output?

这应该这样做:

let datastat = {
  "statisticData": {
    "lastStatisticOne": {
      "min": 0,
      "max": 10
    },
    "lastStatisticTwo": {
      "min": 0,
      "max": 20
    },

    "firstStatisticOne": {
      "min": 0,
      "max": 30,
    },
    "firstStatisticTwo": {
      "min": 0,
      "max": 40,
    },
  }
}

let newStat = Object.keys(datastat.statisticData).reduce((acc, key) => {
  let vals = Object.entries(datastat.statisticData[key]).map(([key, val]) => ({
    name: key,
    value: val
  }))
  acc.push({
    [key]: vals
  });
  return acc;
}, []);

这是 Chris G 提供的解决方案,效果很好。 谢谢!

const statisticNew = Object.entries(datastat.statisticData).map(([key, val]) => ({
  [key]: Object.entries(val).map(([name, value]) => ({ name, value }))
}));

您可以遍历 dataStat 的键并获得预期的 output ,如下所示:

                let statisticNew = [];
                Object.keys(datastat['statisticData']).map(function (item) {
                  let stat_obj = {};
                  let temp_arr  = [];
                  let inner_dict = datastat['statisticData'][item];
                  Object.keys(inner_dict).map(function (inner_item) {
                      let temp_obj = {};
                      temp_obj['name'] = inner_item;
                      temp_obj['value'] = inner_dict[inner_item];
                      temp_arr.push(temp_obj)
                  });
                  stat_obj[item] = temp_arr;
                  statisticNew.push(stat_obj);
                });
                console.log(statisticNew)

您可以首先使用reduce function 减少值,然后相应地mapping它。 方法如下:

 var datastat = { "statisticData": { "lastStatisticOne": { "min": 0, "max": 10 }, "lastStatisticTwo": { "min": 0, "max": 20 }, "firstStatisticOne": { "min": 0, "max": 30, }, "firstStatisticTwo": { "min": 0, "max": 40, }, }, "statisticValue": []}; reducedValue = Object.entries(datastat.statisticData).reduce((acc, [key, value])=>{ acc[key] = acc[key] || []; for(const [k, v] of Object.entries(value)){ acc[key].push({name: k, value:v}) } return acc; },{}); var result = Object.entries(reducedValue).map(([k,v])=>({[k]:v})); console.log(result)

暂无
暂无

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

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