JavaScript Split array of objects into groups by week split array objects,split array objects javascript,split objects array

I have an array of objects that contain data like this

[{
  date: "01-01-2017 00:00:00",
  dataField1: "",
  dataField2: ""
},
{
  date: "01-02-2017 00:00:00",
  dataField1: "",
  dataField2: ""
},
{
  date: "01-15-2017 00:00:00",
  dataField1: "",
  dataField2: ""
},
{
  date: "01-16-2017 00:00:00",
  dataField1: "",
  dataField2: ""
},
{
  date: "01-15-2018 00:00:00",
  dataField1: "",
  dataField2: ""
},
{
  date: "01-16-2018 00:00:00",
  dataField1: "",
  dataField2: ""
}]]

UPD: need to sort by year and month, not just month. I need to split it into an array of arrays where objects are grouped by weeks and years. something like this:

[
  [{
     date: "01-01-2017 00:00:00",
     dataField1: "",
     dataField2: ""
   },
   {
     date: "01-02-2017 00:00:00",
     dataField1: "",
     dataField2: ""
   }
  ],
  [{
     date: "01-15-2017 00:00:00",
     dataField1: "",
     dataField2: ""
   },
   {
     date: "01-16-2017 00:00:00",
     dataField1: "",
     dataField2: ""
   }],
   [
    {
       date: "01-15-2018 00:00:00",
       dataField1: "",
       dataField2: ""
    },
    {
       date: "01-16-2018 00:00:00",
       dataField1: "",
       dataField2: ""
    }
   ]
]

is there a simple way with good performance to make this sorting with javascript?

Answer:1

If you'll consider using something like momentjs, you can use the format function. This way you can actually group by any of the available date format tokens listed here.

function datesGroupByComponent(dates, token) {
  return dates.reduce(function(val, obj) {
    let comp = moment(obj['date'], 'MM/DD/YYYY').format(token);
    (val[comp] = val[comp] || []).push(obj);
    return val;
  }, {});
}

const dates = [
  { date: "01-01-2017 00:00:00" },
  { date: "01-02-2017 00:00:00" },
  { date: "01-15-2017 00:00:00" },
  { date: "01-16-2017 00:00:00" }
];

/* https://momentjs.com/docs/#/displaying/format */

console.log('D', datesGroupByComponent(dates, 'D')); // Day of Month
console.log('w', datesGroupByComponent(dates, 'w')); // Week of Year
console.log('W', datesGroupByComponent(dates, 'W')); // Week of Year (ISO)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Answer:2

I am using OpenShift and Node.js I am trying to get the average - rating for each result but I cant get the response to work even though the console reports correctly. I get 3.9454323 into the ...

I am using OpenShift and Node.js I am trying to get the average - rating for each result but I cant get the response to work even though the console reports correctly. I get 3.9454323 into the ...

  1. openshift node express

I try to post a form using ajax the current code does not really work. When I press the save button the form get submitted n + 1 times. i.e After refreshing the page it submit once, next time I submit ...

I try to post a form using ajax the current code does not really work. When I press the save button the form get submitted n + 1 times. i.e After refreshing the page it submit once, next time I submit ...

  1. submit form with javascript
  2. submit form with jquery
  3. submit form with ajax
  4. submit form with button
  5. submit form with link
  6. submit form with anchor
  7. submit form with a tag
  8. submit form with button outside form
  9. submit form with enter key
  10. submit form with get method
  11. submit form with php
  12. submit form with react
  13. submit form with input type button
  14. submit form with fetch
  15. submit form with curl
  16. submit form with selenium python
  17. submit form with enter key react
  18. submit form with value javascript
  19. submit form with multiple inputs
  20. submit form with python requests

I am developing a chatbot using Dialogflow, I would like to throw a message to user when the chatbot doesn't understand the user input for three times in a row and for the forth time respond with a ...

I am developing a chatbot using Dialogflow, I would like to throw a message to user when the chatbot doesn't understand the user input for three times in a row and for the forth time respond with a ...

  1. throw custom message java
  2. throw custom message c#
  3. throw custom exception message c#
  4. throw custom exception message java
  5. throw custom error message c#
  6. throw custom error message java
  7. powershell throw custom message
  8. throw exception custom message
  9. throw custom error message
  10. sql server throw custom message
  11. throw new exception custom message
  12. throw new exception custom message c#
  13. sql throw custom error message
  14. javascript throw custom error message
  15. sql throw with custom message

I have defined a function as following : function getCurrentComponent(){ if($rootRouter._currentInstruction){ return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(...

I have defined a function as following : function getCurrentComponent(){ if($rootRouter._currentInstruction){ return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(...

  1. function handle to symbolic matlab
  2. function handle to string matlab
  3. function_handle with value
  4. function handle with multiple variables matlab
  5. function handle with two variables
  6. function_handle with value matlab
  7. function handle with parameters matlab
  8. string handling function
  9. function handle for loop matlab
  10. function handle with multiple outputs
  11. handle to function matlab
  12. function handle to symbolic
  13. function handle with multiple variables
  14. function handle to double
  15. function handle with arguments matlab
  16. function handle to matrix
  17. function handle with array
  18. function handle to