Fred
Fred

Reputation: 202

Split array by months

I have a json where there are data only for the current year, the date is in ISODATE format, how could I, angular 1 separate the json objects by month, for example, throw the data to another json, corresponding to the month defined on the date of the variable.

I did the following:

.then(function(resp) {
vm.acoesAno = resp.data
angular.forEach(vm.acoesAno, function(value, key){
    angular.forEach(vm.acoesAno, function(value, key){
        const date = new Date(value.data)
        const mes = date.getMonth()
        if (mes == 0){
            vm.jan = vm.acoesAno[key]
        } else if (mes == 1){
            vm.fev = vm.acoesAno[key]
        } else if (mes == 2){
            vm.mar = vm.acoesAno[key]
        } else if (mes == 3){
            vm.abr = vm.acoesAno[key]
        } else if (mes == 4){
            vm.mai = vm.acoesAno[key]
        } else if (mes == 5){
            vm.jun = vm.acoesAno[key]
        } else if (mes == 6){
            vm.jul = vm.acoesAno[key]
        } else if (mes == 7){
            vm.ago = vm.acoesAno[key]
        } else if (mes == 8){
            vm.set = vm.acoesAno[key]
        } else if (mes == 9){
            vm.out = vm.acoesAno[key]
        } else if (mes == 10){
            vm.nov = vm.acoesAno[key]
        } else if (mes == 11){
            vm.dez = vm.acoesAno[key]
        }
    })  
})  

console.log("mes de maio: ", vm.mai)}

There are two objects in the json vm.acoesAno that are registered with the month of May, but in the variable outside the forEach I get only 1 object, the variable inside the forEach stores the two objects.

JSON DATA vm.acoesAno:

{
    data : "2018-05-06T03:00:00.000Z",
    longitude : "-52.018375",
    latitude : "-27.226520",
    dataCadastro : "2018-05-22T02:57:01.443Z"
}

But there are several objects with different dates in that json.

@vol7ron

return from console.log(grouped[4])

Upvotes: 1

Views: 2501

Answers (2)

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22885

You can use Array.prototype.filter and regex to match specific month to filter data

// if you want to make it efficient, you can do something

  .then(function(resp) {
    const data = resp.data;
    vm.jan = [];
    vm.fev = [];
    // initialize all arrays to empty array
    vm.acoesAno = data;
    data.forEach(item => {
       if (/(.*)-01-(.*)/.test(item.data)){
          vm.jan.push(item);
       } else if (/(.*)-02-(.*)/.test(item.data)){
         vm.fev.push(item);
       } else if (/(.*)-03-(.*)/.test(item.data)){
          vm.mar.push(item);
       }
      // and so on 
    });

// 2nd method

.then(function(resp) {
    const data = resp.data;
    vm.acoesAno = data;
    vm.jan = data.filter(item => /(.*)-01-(.*)/.test(item.data);
    vm.fev = data.filter(item => /(.*)-02-(.*)/.test(item.data);
    vm.mar = data.filter(item => /(.*)-03-(.*)/.test(item.data);
    vm.abr = data.filter(item => /(.*)-04-(.*)/.test(item.data);
    // and so on for all other months

});

Upvotes: 1

vol7ron
vol7ron

Reputation: 42109

Not too familiar with Angular anymore, but:

  • you should be able to pass the object to the iterator function
  • you probably don't need to use the month names as keys, just use their integers
  • I'm not sure why two loops were used in this case — wouldn't 1 be sufficient?
  • you only have one value because you are using an assignment (vm.mai = vm.acoesAno[key]) and not adding to a collection or array, which means you're overwriting on each iteration

.then(function(resp) {
  vm.acoesAno = resp.data
  let grouped = {}
  angular.forEach(vm.acoesAno, function(value, key, obj) {
    const date = new Date(value.data)
    const mes = date.getMonth()
    if (typeof grouped[+mes] == 'undefined')
      grouped[+mes]=[]

    grouped[+mes].push(obj);
  })

  console.log("mes de maio: ", grouped[4])
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

Upvotes: 0

Related Questions