0

i have a Json object that basicly is a array and in each array i want to iterate over the propertys, so i did something like this:

for(var x = 0;x<dados.feeds.length;x++){
        for(var y in dados.feeds[x]){
            console.log(dados.feeds[x][y]);
            /*if(data == dataInicial){
                console.log("a");
            }*/
        }
    }

that console.log(dados.feeds[x][0]) gives me undefined, i just want the first element on each x row, how can i get it ?

json object dados

{
  "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"
    }
  ]
}
  • 1
    hard to comment without seeing what `dados.feeds` looks like – bitten Dec 17 '16 at 11:29
  • edited the question –  Dec 17 '16 at 11:33
  • 1
    it should be `console.log(dados.feeds[x].created_at);` – Bla... Dec 17 '16 at 11:36
  • oh, i get it now. you can't access the first property in the objects inside the feeds array as objects aren't sorted. `y` in `for(var y ..)` gives you the objects key, so `created_at`, `entry_id` and so on. `console.log(dados.feeds[x].created_at);` would give you what you want – bitten Dec 17 '16 at 11:36
  • so why you use outer loop if you want get only `feeds`? Iterate in `dados.feeds` only – Artem Ilchenko Dec 17 '16 at 11:37

4 Answers4

0

How is your data looks like? If it's similar to

var dados = {
  feeds: [ {a: 'aa'}, {b: 'bb'} ]
}

then you can try this:

dados.feeds.forEach(feed => {
  Object.keys(feed).forEach((key, index) => {
    if(index === 0) { console.log(feed[key]) } // aa bb...
  })
})

HTH

The Reason
  • 7,705
  • 4
  • 24
  • 42
0

It should be console.log(dados.feeds[x].created_at);.

Bla...
  • 7,228
  • 7
  • 27
  • 46
0

Although you shouldn't rely on Javascript object key order, here is a solution that will return the value of the first key in each entry in the feeds array;

var dados = {
  "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"
    }
  ]
};

var con = document.getElementById('console');
for (var i = 0; i < dados.feeds.length; i++) {
    var firstKey = Object.keys(dados.feeds[i])[0];
    con.innerHTML += '<p>' + dados.feeds[i][firstKey] + '</p>';
}

/*
Prints;

2014-02-26T12:42:49-05:00

2014-02-26T12:43:04-05:00

*/

Here is a fiddle with the working code.

Community
  • 1
  • 1
tkounenis
  • 665
  • 4
  • 18
0

The code as you have it runs fine, everything traces out, nothing is undefined (I tried it).

But, console.log[x][0] will trace undefined, because:

var x = 0;
var obj = feeds[x]; // this will be the first object from the feeds array.
console.log ( obj [ 0 ] );

You probably see it now; that will trace undefined, because that obj has no 0 property. It's an object with keys/values.

So this would work:

var each = 'created_at';
console.log ( obj [ each ] );

And this:

for ( var each in obj ) {
   console.log ( each, obj [ each ] );
}
Tim Consolazio
  • 4,802
  • 2
  • 19
  • 28