Set amount of returns per function that filters JSON

Asked

Viewed 33 times

0

I have the following code to filter one JSON, it works perfectly, the only problem I’m having is to choose the number of results displayed. (currently only displays a single result).

var elemento = document.getElementById("galeria");
var SearchTag = function (jogo) {
        var i = 0;
        for (i = i; wallpapers.length > i; i += 1) {
            if (wallpapers[i].jogo === jogo) {
                return wallpapers[i];
            }
        }

        return null;
    };

    var wall = SearchTag('tag-para-filtrar');
    if (wall)
    {

    elemento.innerHTML +=
  "" + wall.id + "" +
  "" + wall.thumburl + "" +
  "" + wall.nome + "" +
  "" + wall.autor + "" +
  "" + wall.protecao + "" +
  "" + wall.tags + "" +
  "" + wall.jogo + "" ;
    }

1 answer

1


Change your function as follows:

var SearchTag = function (jogo) {
    var arr = arrary();
    for (var i = 0; wallpapers.length > i; i ++) {
        if (wallpapers[i].jogo === jogo) {
            arr.push(wallpapers[i]);
        }
    }

    return arr;
};

Now your return will be an Array of results, or an empty array. Treat it as follows:

var wall = SearchTag('tag-para-filtrar');
for(var i = 0; i < wall.length; i++)
{

elemento.innerHTML +=
"" + wall[i].id + "" +
"" + wall[i].thumburl + "" +
"" + wall[i].nome + "" +
"" + wall[i].autor + "" +
"" + wall[i].protecao + "" +
"" + wall[i].tags + "" +
"" + wall[i].jogo + "" ;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.