0

I am trying to parse a json result but I keep getting duplicate values in my array. It seems like the code is running twice . Here is my code any feedback would be appreciated

var channels = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas", "brunofin", "comster404"]
var clientID = "######";
var allChannels = [];
var feedReturn = [];
$(document).ready(function() {
    for (var i = 0; i < channels.length; i++) {
        allChannels[i] = channels[i];
        //allChannels[i][0] = channels[i]
        $.getJSON("https://api.twitch.tv/kraken/streams/" + channels[i] + "?
            client_id = sbp1wnku2j32dtnjj4qefhslopxq8s ",function(data){
            $.each(data, function(i) {
                if (data.stream == null) {
                    feedReturn.push(["Null", data._links.channel]);
                } else {
                    feedReturn.push(["Not Null", data._links.channel]);
                }
                //console.log(data.stream);
                // console.log(data._links.channel);
                //feedReturn.push(data.stream,data._links.channel);
            })
        })
}
});
console.log(feedReturn);
1
  • What troubleshooting has been done and what is the question? Commented Feb 16, 2017 at 23:56

1 Answer 1

1

You are calling $.each() on the data object, which will return an array with a length equal to the number of properties on that object. Since the API data returned has two properties (_links, stream) it will run the .push() 2 times for every API call. Simply removing the $.each() will solve your issue with duplicate records in your array.

$.getJSON("https://api.twitch.tv/kraken/streams/"+channels[i]+"?client_id=sbp1wnku2j32dtnjj4qefhslopxq8s",function(data){
  if(data.stream == null){

    feedReturn.push(["Null",data._links.channel]); 

  } else {

    feedReturn.push(["Not Null",data._links.channel]);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.