[{'time': "2016-01-01T12:10", 'value': 2000}, {'time': "2016-01-01T12:30", 'value': 1200}, {'time':"2016-02-02T14:00", 'value': 550}, {'time':"2016-02-02T15:00", 'value': 1000}]; 

Good day. Please tell me how best to group this array into an array of view objects on JS

 [ {date: '2016-01-01', hours: '12:00 PM', valye: {'time': "2016-01-01T12:10", 'value': 2000}, {'time': "2016-01-01T12:30", 'value': 2000} }, ... ] 

    3 answers 3

    It will not work at one time, it is better to use an intermediate dictionary with a key in the form of date + hours without minutes:

     moment = require("moment"); var arr = [{'time': "2016-01-01T12:10", 'value': 2000}, {'time': "2016-01-01T12:30", 'value': 1200}, {'time':"2016-02-02T14:00", 'value': 550}, {'time':"2016-02-02T15:00", 'value': 1000}]; var grouped = {}; arr.forEach(function (obj) { var m = moment(obj.time); var key = m.format("YYYY-MM-DD HH"); if (key in grouped) grouped[key].push(obj); else grouped[key] = [obj]; }) console.log(grouped); var result = []; for (key in grouped) { var obj = grouped[key]; var m = moment(obj.time); var val = { date: m.format("YYYY-MM-DD"), time: m.format("hh:00A"), value: obj }; result.push(val); } console.log(result); 

      So do not work. In the property of the value object, you can either write an array with objects, or specify a specific name for each object, for example, like this:

       var date = '2016-01-01', hours = '12:00 PM', data = [ {'time': "2016-01-01T12:10", 'value': 2000}, {'time': "2016-01-01T12:30", 'value': 1200}, {'time':"2016-02-02T14:00", 'value': 550}, {'time':"2016-02-02T15:00", 'value': 1000} ]; var output = [], obj = {}; obj.data = date; obj.hours = hours; obj.value = {}; for (var i = 0; i < data.length; i++) { obj.value[i] = data[i]; } output.push(obj); console.log(output); 

      Jsfiddle demo

        See what was invented:

         var dates = [{'time': "2016-01-01T12:10", 'value': 2000}, {'time': "2016-01-01T12:30", 'value': 2000}, {'time': "2016-01-01T13:10", 'value': 2000}, {'time': "2016-01-02T13:00", 'value': 1200},, {'time':"2016-02-02T15:00", 'value': 1000}, {'time':"2016-02-12T14:00", 'value': 550}]; function calc(dates) { var responses = {}; dates.forEach(function(d, i){ var date = new Date(d.time); var day = date.getDate(); var hour = date.getHours(); var key = `${date.getHours()}-${date.getDate()}`; var optionsHours = { hour: 'numeric', minute: 'numeric' }; var optionsDay = { year: 'numeric', month: 'long', day: 'numeric' }; var response = responses[key]; if (!response) { response = responses[key] = []; } response.push( { 'day': date.toLocaleString( "en-US", optionsDay), 'hours': date.toLocaleString( "en-US", optionsHours), 'value': d } ); }); console.log(responses); return responses; } calc(dates); 

        I hope that it is useful to someone else.

        Jsfiddle demo

        Jsfiddle demo2

        • the key will match if there are different months in the array, for example: {'time': "2016-01-01T12:30", 'value': 2000} and {'time': "2016-02-01T12:30", 'value': 2000}, will fall into one group - Grundy
        • and add that for the language you used, because in javascript this declaration var optionsDay: TimeOptions = { is a syntax error - Grundy
        • I wrote for Tipe Script forgot to remove the type definition. And about the key you are undoubtedly right. I just data will not come more than a month. But I think it will not be a problem to fix this method for a month and a year. - vlzkonopatov