Union of two variables to create a third array

2

Address where I am writing the code (no need to enter).

I am making a page where the description of several users of twitchTV appears, corresponding to a freecodecamp exercise [266]. The fact is that to automate the process and not having to write each web + user manually, it has occurred to me to use three variables, corresponding to 1) Array with list of users (list). 2) String with the basic url (url) 3) String with the alternative url in case they are not online, since it changes (urlAlt). So I wanted to put everything together by means of a for loop, which I do not know if it is appropriate to put it there without more, but it is the only thing that occurs to me. The problem is that console.log in of for , even though it returns all the urls, it does it separately and not together, so it should not to be working and, if I get the console.log of for , only returns me the last one ( link , although I think that in both cases the result is the same).

I guess it's because when you restart the loop at the beginning of everything you have the command to empty the array ( var lisUrl = []; ), but I can not find a way to fix it.

var lista = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
var url = "https://api.twitch.tv/kraken/streams/";
var urlAlt = "https://api.twitch.tv/kraken/channels/";


for (var i = 0; i < lista.length; i++) {
    var lisUrl = [];
    var lisAlt = [];
    lisUrl.push(url + lista[i]); // Lo he intentado tambien con: lisUrl += lisUrl.push(url + lista[i])
    lisAlt.push(url + lista[i]);

}
console.log(lisUrl);

So here's the question. What I put below is not part of the query, but I add it in case someone wants to clarify it:

The second part of the code is this:

for (var i = 0; i < lista.length; i++) {
    $.getJSON(url + lista[i], function(data) {

        if (data.stream !== null) {
            $("#jugadores").prepend("<div>" + data.stream.viewers + "</div>");
        } //if
        else if (data.stream === null) {
            $.getJSON(urlAlt + lista[i], function(data) {
                $("#jugadores").prepend("<div>" + data.status + "</div>");
            }); //getjson2
        } //else if
    }); //getjson1
} //for


}); //ready

It consists in first verifying if with a url the data can be extracted -only works if the user is online- and, if not, using the alternative url to access them. I would like to know if I can have problems if one of the users is offline because then I would just check the else if for the rest of the list, or if you are going to test both conditionals with the whole list.

    
asked by Alsun Asfalto 28.08.2016 в 07:19
source

1 answer

1
  

I guess it's because when you restart the loop at the beginning of everything you have the command to empty the array ( var lisUrl = []; )

It's exactly that. Initialize the variables before of the loop:

var lista = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"];
var url = "http://www.miweb.com/streams/";
var urlAlt = "http://www.miweb.com/channels/";

//Inicializar nuevos arrays
var lisUrl = [];
var lisAlt = [];

//Popularlos
for (var i = 0; i < lista.length; i++) {
    lisUrl.push(url + lista[i]);
    lisAlt.push(urlAlt + lista[i]);
}

//Mostrar contenido
console.log(lisUrl, lisAlt);
  

I would like to know if I can have problems if one of the users is offline because then I would only check the else if for the rest of the list, or if you are going to test both conditionals with the whole list.

It checks for each call to $.getJSON() , that is to say for each address in the list.

On the other hand, a detail, it is not necessary to use a else if , since the only value it can have at that point is null .

if (valor !== null) {
    // código
} else {
    // código si es null
}
    
answered by 28.08.2016 / 08:20
source