在对象数组中使用相同的键对值进行分组

时间:2017-03-15 16:01:27

标签: javascript highcharts

要在highcharts.js中显示数据,我需要转换以下数据:

"personas": [
      {
        "category":"Persona1",
        "month":"6",
        "week":"24",
        "day":"18",
        "dayaverage":0.5,
        "weekaverage":1.33333333333333,
        "monthaverage":1.53571428571429
      },
      {
        "category":"Persona2",
        "month":"6",
        "week":"24",
        "day":"18",
        "dayaverage":0.146477031224456,
        "weekaverage":0.194758246723904,
        "monthaverage":0.601273296708939
      },
      {
        "category":"Persona3",
        "month":"6",
        "week":"24",
        "day":"18",
        "dayaverage":1.25559947299078,
        "weekaverage":1.43618513323983,
        "monthaverage":0.998426393184655
      },
      {
        "category":"Persona4",
        "month":"6",
        "week":"24",
        "day":"18",
        "dayaverage":0.799332962757087,
        "weekaverage":0.923262727610554,
        "monthaverage":0.769477297163179
      },
      {
        "category":"Persona5",
        "month":"6",
        "week":"24",
        "day":"18",
        "dayaverage":0.669041769041769,
        "weekaverage":0.67394482002558,
        "monthaverage":0.670944920469891
      },
      {
        "category":"Persona6",
        "month":"6",
        "week":"24",
        "day":"18",
        "dayaverage":0.656381486676017,
        "weekaverage":0.722973507315144,
        "monthaverage":0.69689774371321
      },
      {
        "category":"Persona7",
        "month":"6",
        "week":"24",
        "day":"18",
        "dayaverage":0.540495407737267,
        "weekaverage":0.576413277444205,
        "monthaverage":0.693495281755596
      }
    ]

采用以下格式:

[
   {
      name: 'dayaverage',
      data: [0.5, 0.146477031224456, 1.25559947299078, 0.799332962757087, 0.669041769041769, 0.656381486676017, 0.540495407737267]
   },
   {
     name: 'weekaverage',
     data: [1.33333333333333, 0.194758246723904, 1.43618513323983, 0.923262727610554, 0.67394482002558, 0.722973507315144, 0.576413277444205]
   }, {
     name: 'monthaverage',
     data: [1.53571428571429, 0.601273296708939, 0.998426393184655, 0.769477297163179, 0.670944920469891, 0.69689774371321, 0.693495281755596]
   }
].

我正在做的就是将dayaverageweekaveragemonthaverage值分组到一个数组中,并使用名称键值对指定它们的含义。

我在写这篇文章时遇到了麻烦,因为父函数将使用一系列条件进行调用(对于上面的示例,它是:criteria = ['dayaverage', 'weekaverage', 'monthaverage'];),这可能会改变。

感谢任何帮助,谢谢

5 个答案:

答案 0 :(得分:2)

您可以将数组用于所需属性,并使用数据构建数组。

function getGrouped(array, groups) {
    var grouped = groups.map(function (a) {
            return { name: a, data: [] };
        });

    array.personas.forEach(function (a) {
        groups.forEach(function (k, i) {
            grouped[i].data.push(a[k]);
        });
    });
    return grouped;
}

var data = { personas: [{ category: "Persona1", month: 6, week: 24, day: 18, dayaverage: 0.5, weekaverage: 1.33333333333333, monthaverage: 1.53571428571429 }, { category: "Persona2", month: 6, week: 24, day: 18, dayaverage: 0.146477031224456, weekaverage: 0.194758246723904, monthaverage: 0.601273296708939 }, { category: "Persona3", month: 6, week: 24, day: 18, dayaverage: 1.25559947299078, weekaverage: 1.43618513323983, monthaverage: 0.998426393184655 }, { category: "Persona4", month: 6, week: 24, day: 18, dayaverage: 0.799332962757087, weekaverage: 0.923262727610554, monthaverage: 0.769477297163179 }, { category: "Persona5", month: 6, week: 24, day: 18, dayaverage: 0.669041769041769, weekaverage: 0.67394482002558, monthaverage: 0.670944920469891 }, { category: "Persona6", month: 6, week: 24, day: 18, dayaverage: 0.656381486676017, weekaverage: 0.722973507315144, monthaverage: 0.69689774371321 }, { category: "Persona7", month: 6, week: 24, day: 18, dayaverage: 0.540495407737267, weekaverage: 0.576413277444205, monthaverage: 0.693495281755596 }] };

console.log(getGrouped(data, ['day', 'dayaverage', 'weekaverage', 'monthaverage']));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您可以使用.map().reduce()这样执行此操作:

此外,要使用动态属性,您可以使用括号语法[])来访问对象的属性。在这里,您可以.map()将您的条件列表添加到所需的结构中,并使用.reduce()计算数据。

编辑 - 修复了生成的数据结构,以准确输出所需的结果

var personas = [{
    "category": "Persona1",
    "month": "6",
    "week": "24",
    "day": "18",
    "dayaverage": 0.5,
    "weekaverage": 1.33333333333333,
    "monthaverage": 1.53571428571429
  },
  {
    "category": "Persona2",
    "month": "6",
    "week": "24",
    "day": "18",
    "dayaverage": 0.146477031224456,
    "weekaverage": 0.194758246723904,
    "monthaverage": 0.601273296708939
  },
  {
    "category": "Persona3",
    "month": "6",
    "week": "24",
    "day": "18",
    "dayaverage": 1.25559947299078,
    "weekaverage": 1.43618513323983,
    "monthaverage": 0.998426393184655
  },
  {
    "category": "Persona4",
    "month": "6",
    "week": "24",
    "day": "18",
    "dayaverage": 0.799332962757087,
    "weekaverage": 0.923262727610554,
    "monthaverage": 0.769477297163179
  },
  {
    "category": "Persona5",
    "month": "6",
    "week": "24",
    "day": "18",
    "dayaverage": 0.669041769041769,
    "weekaverage": 0.67394482002558,
    "monthaverage": 0.670944920469891
  },
  {
    "category": "Persona6",
    "month": "6",
    "week": "24",
    "day": "18",
    "dayaverage": 0.656381486676017,
    "weekaverage": 0.722973507315144,
    "monthaverage": 0.69689774371321
  },
  {
    "category": "Persona7",
    "month": "6",
    "week": "24",
    "day": "18",
    "dayaverage": 0.540495407737267,
    "weekaverage": 0.576413277444205,
    "monthaverage": 0.693495281755596
  }
];
var criteria = ['dayaverage', 'weekaverage', 'monthaverage'];

function getMerged(objArr, criteria) {
    var dataMap = objArr.reduce(function (result, current) {
        criteria.forEach(function (elem) {
            if (result[elem] != undefined) {
                result[elem].push(current[elem]);
            }
            else {
                result[elem] = [current[elem]];
            }
        });
        return result;
    }, {});

    return criteria.map(function (elem) {
        return {
            name: elem,
            data: dataMap[elem]
        };
    });
}

console.log(getMerged(personas, criteria));

答案 2 :(得分:1)

使用Array#forEach解决问题的方法之一。

var json = {personas:[{category:"Persona1",month:"6",week:"24",day:"18",dayaverage:.5,weekaverage:1.33333333333333,monthaverage:1.53571428571429},{category:"Persona2",month:"6",week:"24",day:"18",dayaverage:.146477031224456,weekaverage:.194758246723904,monthaverage:.601273296708939},{category:"Persona3",month:"6",week:"24",day:"18",dayaverage:1.25559947299078,weekaverage:1.43618513323983,monthaverage:.998426393184655},{category:"Persona4",month:"6",week:"24",day:"18",dayaverage:.799332962757087,weekaverage:.923262727610554,monthaverage:.769477297163179},{category:"Persona5",month:"6",week:"24",day:"18",dayaverage:.669041769041769,weekaverage:.67394482002558,monthaverage:.670944920469891},{category:"Persona6",month:"6",week:"24",day:"18",dayaverage:.656381486676017,weekaverage:.722973507315144,monthaverage:.69689774371321},{category:"Persona7",month:"6",week:"24",day:"18",dayaverage:.540495407737267,weekaverage:.576413277444205,monthaverage:.693495281755596}]},
    criteria = ['dayaverage', 'weekaverage', 'monthaverage'],
    arr = criteria.reduce(function(s,a){
        s.push({name: a, data: []});
        return s;
      }, []);

    arr.forEach(function(v) {
      json.personas.forEach(function(c) {
        v.data.push(c[v.name]);
      })
    })
    console.log(arr);

答案 3 :(得分:0)

试试这个:

 var criteria = ['dayaverage', 'weekaverage', 'monthaverage']; //your dynamic criteria
    var arr= []; //the filtered array you want
    criteria.forEach(function(criterium){
        // for each criterium you create a new object that you add to arr
        arr.push({
            name: criterium,
            data: []
        });
        // then you populate the data field of the newly created field by browsing your big array "personas" that you need to parse before btw
         personas.forEach(function (persona) {
            arr[arr.length-1].data.push(persona[criterium]);
        });
    });

答案 4 :(得分:0)

您可以将键映射到对象数组,并通过映射过程中的当前键映射每个对象的值。

var data = getData();
var modified = modify(data, 'personas', ['dayaverage', 'weekaverage', 'monthaverage']);

console.log(JSON.stringify(modified, null, 2));

function modify(data, root, keep) {
  data = data[root] != null ? data[root] : data;
  var keys = Object.keys(data[0]);
  if (keep != null && keep.length > 0)
    keys = keys.filter(key => keep.indexOf(key) > -1);
  return keys.map(key => {
    return {
      name: key,
      data: data.map(item => item[key])
    }
  });
}

function getData() {
  return {
    "personas": [{
      "category": "Persona1",
      "month": "6",
      "week": "24",
      "day": "18",
      "dayaverage": 0.5,
      "weekaverage": 1.33333333333333,
      "monthaverage": 1.53571428571429
    }, {
      "category": "Persona2",
      "month": "6",
      "week": "24",
      "day": "18",
      "dayaverage": 0.146477031224456,
      "weekaverage": 0.194758246723904,
      "monthaverage": 0.601273296708939
    }, {
      "category": "Persona3",
      "month": "6",
      "week": "24",
      "day": "18",
      "dayaverage": 1.25559947299078,
      "weekaverage": 1.43618513323983,
      "monthaverage": 0.998426393184655
    }, {
      "category": "Persona4",
      "month": "6",
      "week": "24",
      "day": "18",
      "dayaverage": 0.799332962757087,
      "weekaverage": 0.923262727610554,
      "monthaverage": 0.769477297163179
    }, {
      "category": "Persona5",
      "month": "6",
      "week": "24",
      "day": "18",
      "dayaverage": 0.669041769041769,
      "weekaverage": 0.67394482002558,
      "monthaverage": 0.670944920469891
    }, {
      "category": "Persona6",
      "month": "6",
      "week": "24",
      "day": "18",
      "dayaverage": 0.656381486676017,
      "weekaverage": 0.722973507315144,
      "monthaverage": 0.69689774371321
    }, {
      "category": "Persona7",
      "month": "6",
      "week": "24",
      "day": "18",
      "dayaverage": 0.540495407737267,
      "weekaverage": 0.576413277444205,
      "monthaverage": 0.693495281755596
    }]
  };
}
.as-console-wrapper { top: 0; max-height: 100% !important; }