-1

So, I'm using nodejs to parse json, and I'm wanting to get an object that's an array and use the values in a for each loop.

JSON I'm getting: http://tmi.twitch.tv/group/user/loneztar/chatters

    var cronUsers = client.utils.cronjobs('1 * * * * *', function() {
    console.log('API Test');

    console.log('Response from Twitch Chat:');
    request('http://tmi.twitch.tv/group/user/loneztar/chatters', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        data = JSON.parse(body);
        console.log(data.chatters.viewers);

        //for each string in the viewers

      }
    })
});

Above is the code I'm using to retrieve the data. And when I log to the console I get the following (at time of asking):

[ 'duckziller72',
'adanaran',
'nyckeln',
'diabolicalsheep69',
'audery101',
'gone_nutty',
'k4unl',
'eidokan',
'mattesolo',
'siland',
'nullvoid8',
'troy00114',
'sixdev',
'jake_evans',
'doctoranguus',
'juicegraip',
'k4rush' ]

Now I want to use each of them strings in a for each loop. I've tried using the data var I'm using but I can't get anything working. Would appreciate some help! Feel free to ask for other stuff, I'll happily edit.

  • 3
    I don't understand what your issue is. Are you doing `data.chatters.views.forEach` and not getting the correct output? – Nick Tomlin Jan 17 '15 at 22:40
  • My first time actually using nodejs. I guess I've been using for each wrong. I'll see if I can get something working and if not post back with results. Thanks! – Jake Evans Jan 17 '15 at 22:42
  • 1
    It doesn't look like the problem has anything to do with parsing JSON. What _is_ the problem? – Matt Ball Jan 17 '15 at 22:43
  • @JakeEvans, i'd take a look at the MDN documentation for [Array.prototype.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). You can do something like `data.chatters.views.forEach(function (user) { console.log(user); })` – Nick Tomlin Jan 17 '15 at 22:49
  • That's exactly what I'm looking for (obviously doing stuff with it and not just logging). Thanks very much @NickTomlin :) – Jake Evans Jan 17 '15 at 22:50

1 Answers1

0

There are a few different ways to do this.

Using a loop counter:

var viewers = data.chatters.viewers;
var numViewers = viewers.length;
for (var i = 0; i < numViewers; i++) {
    var viewer = viewers[i];
    console.log(viewer);
}

Using 'for .. in':

var viewers = data.chatters.viewers;
for (var i in viewers) {
    var viewer = viewers[i];
    console.log(viewer);
}

You may have been trying to use this, but the loop variable contains the indices of the array, not the values.

Using Array.prototype.forEach():

data.chatters.viewers.forEach(function(viewer) {
    console.log(viewer);
});

Note that none of this is specific to Node.js or JSON responses.

SvdB
  • 415
  • 3
  • 8
  • [`Why is using “for…in” with array iteration such a bad idea?`](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea) – Xotic750 Jan 18 '15 at 01:15