1

How to group array of objects to array with key indexname and compare with dates array

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],]

I have an array of object and dates array i want to get the following result How to acheive this?

1 Answer 1

1

Here is the pure javascript representation for the output result you want. Might be improved for 1 or 2 less loops:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.