按键对对象数组进行分组,并将分组结果与日期数组进行比较

时间:2018-10-06 07:06:03

标签: javascript arrays

如何将对象数组分组为具有键索引名的数组并与日期数组进行比较

var data= [
    {
      "indexname": "red",
      "data": [
        {
          "date": "2018-09-07",
          "count": 3
        }
      ]
    },
     {
      "indexname": "red",
      "data": [
        {
          "date": "2018-09-05",
          "count": 2
        }
      ]
    },
     {
      "indexname": "red",
      "data": [
        {
          "date": "2018-09-06",
          "count": 10
        }
      ]
    },
     {
      "indexname": "yellow",
      "data": [
        {
          "date": "2018-09-07",
          "count": 6
        }
      ]
    }
  ]
var dates=['2018-08-09','2018-08-07','2018-09-07','2018-09-01','2018-09-06','2018-09-05','2018-09-04','2018-09-03','2018-09-02']
var grouped = _.mapValues(_.groupBy(data.results, 'indexname'),
                          clist => clist.map(index => _.omit(index, 'indexname')));
required format
var result=[['date','red','yellow'],[2018-08-09,0,0],[2018-08-07,0,0],[2018-09-07,3,6],[2018-09-06,10,0],[2018-09-05,5,0],[2018-09-04,0,0],[2018-09-03,0,0],[2018-09-02,0,0],]

我有一个对象数组和日期数组,我想得到以下结果 如何做到这一点?

1 个答案:

答案 0 :(得分:1)

这是您想要的输出结果的纯JavaScript表示形式。可能会减少1或2个循环:

var data = [{
        "indexname": "red",
        "data": [{
            "date": "2018-09-07",
            "count": 3
        }]
    },
    {
        "indexname": "red",
        "data": [{
            "date": "2018-09-05",
            "count": 2
        }]
    },
    {
        "indexname": "red",
        "data": [{
            "date": "2018-09-06",
            "count": 10
        }]
    },
    {
        "indexname": "yellow",
        "data": [{
            "date": "2018-09-07",
            "count": 6
        }]
    }
];

var dates = ['2018-08-09', '2018-08-07', '2018-09-07', '2018-09-01', '2018-09-06', '2018-09-05', '2018-09-04', '2018-09-03', '2018-09-02'];

//Prepair index1 of final array an push to see how many colors are present
var index1 = ['date'];

var colors_sorted_arr = [];
var dates_sorted_arr = [];

//Push data to index1 for colors
for (var i = 0; i < data.length; i++) {
    if (index1.indexOf(data[i].indexname) == -1) {
        index1.push(data[i].indexname)
    }
}

//Seperate out data according to color wise
for (var i = 0; i < data.length; i++) {
    for (var j = 1; j < index1.length; j++) {
        if (data[i].indexname == index1[j]) {
            if (colors_sorted_arr[(j - 1)] == undefined) {
                colors_sorted_arr[(j - 1)] = [];
                colors_sorted_arr[(j - 1)].push(data[i].data[0]);
            } else {
                colors_sorted_arr[(j - 1)].push(data[i].data[0]);
            }
        }
    }
}

//Now push index1 data to final array
dates_sorted_arr[0] = index1;

//For other data to final array loop through dates array , then inside that loop through all colors 
//and then for all colors loop inside each value to check for particular data
for (var i = 0; i < dates.length; i++) {
    dates_sorted_arr[(i + 1)] = [];
    dates_sorted_arr[(i + 1)].push(dates[i]);
    for (var j = 0; j < colors_sorted_arr.length; j++) {
        for (var k = 0; k < colors_sorted_arr[j].length; k++) {
            if (colors_sorted_arr[j][k].date == dates[i]) {
                dates_sorted_arr[(i + 1)].push(colors_sorted_arr[j][k].count);
            }
        }
    }
    for (var l = 0; l < index1.length; l++) {
        if (dates_sorted_arr[(i + 1)].length != index1.length) {
            dates_sorted_arr[(i + 1)].push(0);
        }
    }
}

//After creating above got final result what you want
console.log(dates_sorted_arr);