Set amount of results returned by function that filters JSON

0

I have the following code to filter a JSON , it works perfectly, the only problem I'm having is to choose the number of results displayed. (currently only displaying 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 + "" ;
    }
    
asked by anonymous 19.07.2016 / 19:48

1 answer

1

Change your role 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. It looks like this:

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 + "" ;
}
    
06.02.2017 / 13:43