处理Java脚本中对象数组的有效方法

时间:2019-06-11 17:46:46

标签: javascript node.js

我想知道从javascript对象数组处理和准备相关数据的最佳方法。我在需要获取数据,然后通过对公用密钥进行分组来处理和转换数据的地方编写API。

示例:- 我通过以下方式获取对象数组

    [
    {
            Name: "test1"
            OS: "Solaris"
            Vendor: "Oracle"
            globalid: "44"
            avgFileSysUtilization: 22.618
            avgcpu: 2.281
            avgmemory: 85.627
            month: "June"
            monthval: "06"
        },
        {
            Name: "test1"
            OS: "Solaris"
            Vendor: "Oracle"
            globalid: "44"
            avgFileSysUtilization: 19.461
            avgcpu: 2.26
            avgmemory: 84.885
            month: "May"
            monthval: "05"
        },
        {
            Name: "test2"
            OS: "Solaris"
            Vendor: "Oracle"
            globalid: "48"
            avgFileSysUtilization: 22.618
            avgcpu: 2.281
            avgmemory: 85.627
            month: "June"
            monthval: "06"
        },
        {
            Name: "test2"
            OS: "Solaris"
            Vendor: "Oracle"
            globalid: "48"
            avgFileSysUtilization: 19.461
            avgcpu: 2.26
            avgmemory: 84.885
            month: "May"
            monthval: "05"
        }
    ]

使用javascript代码,我需要将其转换为

  [{

        Name: "test1"
        OS: "Solaris"
        Vendor: "Oracle"
        globalid: "44"

        data: [

            {

                avgFileSysUtilization: 22.618
                avgcpu: 2.281
                avgmemory: 85.627
                monthval: "June2019"

            }, {

                avgFileSysUtilization: 19.461
                avgcpu: 2.26
                avgmemory: 84.885
                monthval: "May2019"

            }
        ]
    },
    {

        Name: "test2"
        OS: "Solaris"
        Vendor: "Oracle"
        globalid: "48"

        data: [

            {

                avgFileSysUtilization: 22.618
                avgcpu: 2.281
                avgmemory: 85.627
                monthval: "June2019"

            }, {

                avgFileSysUtilization: 19.461
                avgcpu: 2.26
                avgmemory: 84.885
                monthval: "May2019"

            }
        ]
    }
  ]

我有以下代码段可实现预期的输出,我需要知道这是正确的方法还是有更好的方法来完成此任务

var groups = _.groupBy(result, function(value){
   return value.Name + '#' + value.globalid + '#' + value.Vendor + '#' + alue.OS;
});
      var data = [];
      for (let key in groups) {
        let jsonData = {};
        let commonData = key.split("#");
        jsonData["Name"] = commonData[0];
        jsonData["GlobalId"] = commonData[1];
        jsonData["Vendor"] = commonData[2];
        jsonData["OS"] = commonData[3];
        jsonData["Data"] = [];
        var result = groups[key].map(function(obj) {
          jsonData["Data"].push(_.pick(obj, ['avgFileSysUtilization', 'avgcpu','avgmemory','monthval']));
        });
        data.push(jsonData);
      }

1 个答案:

答案 0 :(得分:1)

从表面上看,您的解决方案似乎还不错。它使用了一些不必要的lodash调用和map,它们都有开销,但是除非您有大量数据,否则这无关紧要。

别误会我的意思,lodash非常棒,特别是对于真正复杂的任务。但这并不复杂。这是一个使用单个循环重新格式化数据的解决方案:

const sourceData = [
  {
    Name: "test1",
    OS: "Solaris",
    Vendor: "Oracle",
    globalid: "44",
    avgFileSysUtilization: 22.618,
    avgcpu: 2.281,
    avgmemory: 85.627,
    month: "June",
    monthval: "06"
  },
  {
    Name: "test1",
    OS: "Solaris",
    Vendor: "Oracle",
    globalid: "44",
    avgFileSysUtilization: 19.461,
    avgcpu: 2.26,
    avgmemory: 84.885,
    month: "May",
    monthval: "05"
  },
  {
    Name: "test2",
    OS: "Solaris",
    Vendor: "Oracle",
    globalid: "48",
    avgFileSysUtilization: 22.618,
    avgcpu: 2.281,
    avgmemory: 85.627,
    month: "June",
    monthval: "06"
  },
  {
    Name: "test2",
    OS: "Solaris",
    Vendor: "Oracle",
    globalid: "48",
    avgFileSysUtilization: 19.461,
    avgcpu: 2.26,
    avgmemory: 84.885,
    month: "May",
    monthval: "05"
  }
];

const formattedData = [];
const groupMap = {};

for (let idx = 0; idx < sourceData.length; idx += 1) {
  const key = sourceData[idx].Name + '#' + sourceData[idx].globalid + '#' + sourceData[idx].Vendor + '#' + sourceData[idx].OS;
  let group = groupMap[key];

  if (group === undefined) {
    group = {};
    group.Name = sourceData[idx].Name;
    group.globalid = sourceData[idx].globalid;
    group.Vendor = sourceData[idx].Vendor;
    group.OS = sourceData[idx].OS;
    group.data = [];

    groupMap[key] = group;
    formattedData.push(group);
  }

  group.data.push({
    avgFileSysUtilization: sourceData[idx].avgFileSysUtilization,
    avgcpu: sourceData[idx].avgcpu,
    avgmemory: sourceData[idx].avgmemory,
    month: sourceData[idx].month,
    monthval: sourceData[idx].monthval
  });
}

console.log(formattedData);