Remove empty spaces in arrays in JS

-2

I would like to know how to eliminate the gaps that are in a vector.

The situation is as follows: I need to read all text tags from an HTML page and store them in an array so that I can work on them individually in the future. I created an array that has the tags registered, another to save the tags that are found on the page according to the ones that are registered in the first array. However, white spaces are also saved at each iteration.

var tags = ["h1", "h2", "h3", "h4", "h5", "h6", "h7", "p", "a", "li", "span", "strong", "li"];
        var elementosPag = [];

        var nroElementos = document.body.getElementsByTagName("*").length;  

        for(var i = 0; i<tags.length; i++)
            elementosPag.push(document.body.getElementsByTagName(tags[i]));
    
asked by anonymous 18.01.2018 / 21:47

1 answer

-1

So far I have managed to solve this, but I do not know if it is the most optimized solution:

        var tags = ["h1", "h2", "h3", "h4", "h5", "h6", "h7", "p", "a", "li", "span", "strong", "li"];
        var elementosPag = [];
        var matches = [];
        var nroElementos = document.body.getElementsByTagName("*").length;  

        for(var i = 0; i<tags.length; i++){
            elementosPag.push(document.body.getElementsByTagName(tags[i]));
            if(elementosPag[i].length != 0)
                matches.push(elementosPag[i]);
        }
        for(var i = 0; i<matches.length; i++){
            console.log(matches[i])

        }
    
18.01.2018 / 22:19