0

i have some troubles to solve a problem that is more related to algoritmic then with a specific language. Basicly i have a Json object like this:

 {
  "channel":
  {
    "id": 9,
    "name": "my_house",
    "description": "Netduino Plus connected to sensors around the house",
    "latitude": "40.44",
    "longitude": "-79.996",
    "field1": "Light",
    "field2": "Outside Temperature",
    "created_at": "2010-12-13T20:20:06-05:00",
    "updated_at": "2014-02-26T12:43:04-05:00",
    "last_entry_id": 6060625
  },
  "feeds":
  [
    {
      "created_at": "2014-02-26T12:42:49-05:00",
      "entry_id": 6060624,
      "field1": "188",
      "field2": "25.902335456475583"
    },
    {
      "created_at": "2014-02-26T12:43:04-05:00",
      "entry_id": 6060625,
      "field1": "164",
      "field2": "25.222929936305732"
    }
  ]
}

and i want to create a chart based on the diferent dates(created_at) i want to trace just the last date what i mean is, that i want to get the last field from each date, imagine i have 10 fields all with the created_at:17/12/2016 i want to keep the last, and then continue to iterate and take the last from each date, how can i do it?

i tried this at the moment

 var dataInicial = separaData(dados.feeds[0].created_at);
        console.log(dataInicial);
        var dataPointsConst = []; // para construir o objeto JSON para servir o gráfico
        var somatorio;
        for(var x = 0;x<dados.feeds.length;x++){
                var data = separaData(dados.feeds[x].created_at);
                console.log(data[0]);
                console.log(dataInicial[0]);
                if(data[0] == dataInicial[0]){
                    somatorio = parseInt(dados.feeds[x].field1) + parseInt(dados.feeds[x].field2) + parseInt(dados.feeds[x].field3) + parseInt(dados.feeds[x].field4) + parseInt(dados.feeds[x].field5) + parseInt(dados.feeds[x].field6) + parseInt(dados.feeds[x].field7) + parseInt(dados.feeds[x].field8);
                }
                else{
                    dataPointsConst.push({x:data[0],y:somatorio});
                    dataInicial = data[0].created_at;
                }
        }
  • Look at http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value – Nick H Dec 17 '16 at 14:08
  • it is already sorted by date, i just can not get the last from each date –  Dec 17 '16 at 14:09
  • 1
    I dont know what you mean by 'take the last' though. Take the last element in the 'feeds' array, take the last property of a 'feeds' object? – Nick H Dec 17 '16 at 14:11
  • take the last element from each date, imagine i have 10 feeds with the date of today, i need to get the last, then imagine in my feeds object i have also the tomorrow date 5 times i want the last 2 and so on –  Dec 17 '16 at 14:13
  • If you sort the `feeds` **array** you can just [pop()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) of the last one. If it's an object, there is no last, as there is no order in objects – adeneo Dec 17 '16 at 14:18
  • but in this case the last 1 you specify is 1 date, i can have more diferent dates in my feed array, i want to get the last 1 of every diferent date –  Dec 17 '16 at 14:25

0 Answers0