Tour a json rescuing the last repeated

0

This the Json Object

var data = [
    {anioTtile: "2018", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
    {anioTtile: "2018", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]},
    {anioTtile: "2017", anioData:[10000,0,0,0,0,0,0,0,0,0,0,0]},
    {anioTtile: "2017", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
    {anioTtile: "2017", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]}
]

This is the JavaScript function that I use:

var anios = {};
    var resultado = data.filter(function (e) {
        return anios[e.anioTtile] ? false : (anios[e.anioTtile] = true);
    });

But the result is:

var data = [
        {anioTtile: "2018", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
        {anioTtile: "2017", anioData:[10000,0,0,0,0,0,0,0,0,0,0,0]}
    ]

What I want is to have only the years without repeating and to have the most complete information, referring to anioData , that is; the last array of each year, or if possible, allow me to unify the ones that are repeated

Example:

var data = [
    {anioTtile: "2018", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]},
    {anioTtile: "2017", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]}
]
    
asked by Andrés Cantillo 08.06.2018 в 22:23
source

2 answers

1

To be able to traverse the JSON object correctly, you should use some FOR, and since you want to combine them, you must validate which year it is to join the data that interests you each month and year.

 var data = [
      {anioTtile: "2018", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
      {anioTtile: "2018", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]},
      {anioTtile: "2017", anioData:[10000,0,0,0,0,0,0,0,0,0,0,0]},
      {anioTtile: "2017", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
      {anioTtile: "2017", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]}
    ]
    
  var length = data.length;
  var arrFinal = [
    {
      "anioTtile":"2018",
      "anioData":[0,0,0,0,0,0,0,0,0,0,0,0]
    },
    {
      "anioTtile":"2017",
      "anioData":[0,0,0,0,0,0,0,0,0,0,0,0]
    }
  ]
  
  for (var i = 0; i < length; i++) {
	  var j = 0;

	  console.log(data[i].anioTtile);
      switch(data[i].anioTtile)
        {
          case "2018":
            for (j = 0; j < 12; j++) {
             	arrFinal[0].anioData[j] = arrFinal[0].anioData[j] + (data[i].anioData[j]/1);
            }
            break;
          case "2017":
            for (j = 0; j < 12; j++) {
              arrFinal[1].anioData[j] = arrFinal[1].anioData[j] + (data[i].anioData[j]/1);
            }
            break;
        }
    }

  console.log(JSON.stringify(arrFinal))

To be able to gather the information, I used an auxiliary object array, to be able to fill in the information corresponding to each year and each position.

The clear solution is not very flexible because the idea is to see how you can implement it, and from there you can make it more efficient for more years or more data in each one.

I think that you get the desired result, I remain aware of your comments:)

    
answered by 08.06.2018 в 23:26
0
var data = [
        {anioTtile: "2018", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
        {anioTtile: "2018", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]},
        {anioTtile: "2017", anioData:[10000,0,0,0,0,0,0,0,0,0,0,0]},
        {anioTtile: "2017", anioData:[10000,0,20000,0,0,0,0,0,0,0,0,0]},
        {anioTtile: "2017", anioData:[10000,0,20000,0,50000,0,5000,5000,0,0,0,0]}
    ]  



    var groupBy = function (data, val) {
            return data.reduce(function(grupo, pos) {
                var valor = pos[val];
                grupo[valor] = grupo[valor] || {anioTtile: pos.anioTtile, anioData: []};
                grupo[valor].anioData = pos.anioData;
                return grupo;
            }, {});
        }
        var datas = groupBy(data,'anioTtile');

    console.log(datas);
    
answered by 08.06.2018 в 23:51