How to return the last record of an array with Javascript or jQuery?

4

I'm doing a custom search filter where the user can select the brand and the characteristics of the product and I return the id of them.

But I wanted to return only the last record of my arrays where they contain all the information I need.

I need only return the last record displayed in the console as in the image below. Ex: mark = 25,30,53 and feature: 10: 35: 4

Follow the code:

<a href="#" class="filter-link">
  <div class="label_check">
       <input type="checkbox" id="marca-4" class="filter-check" data-tipo="marca?4" value="4">
       <div class="filter-pretty-check"></div>
       <span class="filter-desc">Nokia</span>
  </div>
</a>

    $('#filtrar-opcoes').on('click',function(){                    
                var tipos = [];
                var caracteristicas = [];
                $.each(($("input[type=checkbox]:checked")),function(index, obj){                                                
                    var $tipo =  $(obj).attr('data-tipo');                                                                                   
                    if($tipo.split('?')[0] == 'marca'){
                        tipos.push($tipo.split('?')[1]);                                    
                        var firstPartUrl = $tipo.split('?')[0] + '=' + tipos.join();                                                                
                        console.log(firstPartUrl);                                                                                                                    
                    }
                    if($tipo.split('?')[0] == 'caracteristica'){
                        caracteristicas.push($tipo.split('?')[1]);                            
                        var lastPartUrl = $tipo.split('?')[0] + '=' + caracteristicas.join(':');
                        console.log(lastPartUrl);                            
                    }
                });
            });

    
asked by anonymous 17.09.2015 / 14:43

4 answers

11

You can do this using pure javascript, like this:

var array = [1, 2, 3, 4, 5];
var ultimo = array[array.length - 1];
document.write(ultimo);

If you prefer to use jQuery, you can do something like:

var array = [1, 2, 3, 4];
var ultimo = $(array).get(-1);
document.write(ultimo);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
17.09.2015 / 14:49
6

Another way to get the last array element, assuming it does not remove the last element from the array , would be to use the slice function pointing to the last element through -1 .

a = [1, 2, 3]
document.write(a.slice(-1)[0])

slice will return the last array element within an array . That's why we use the index 0 .

See:

a.slice(-1); // Imprime: [3]
    
17.09.2015 / 15:15
4

Another way would also be to use the pop function. It removes the last element from the vector and returns it. I do not know if it would be advisable in your case.

See working here.

var vetor1 = new Array(1, 2, 3, 2, 7);
var ultimo = vetor1.pop();
document.write(ultimo);
    
17.09.2015 / 14:55
2

If it is recurring in the project, you can extend the Array by implementing a method for this:

Array.prototype.get = function(index) {
    return this[index < 0 ? this.length + index : index];
};

So, anywhere, to get the last element would just do:

arr.get(-1)

It applies the same logic described in the LINQ response, but in a global in>, you do not have to repeat the code every time you access the last element.

See working:

Array.prototype.get = function(index) {
    return this[index < 0 ? this.length + index : index];
};

Array.prototype.last = function(index) {
    return this.get(-1);
};

Array.prototype.first = function(index) {
    return this[0];
};

const arr = [1, 2, 3];

console.log('Último elemento com get:', arr.get(-1));
console.log('Último elemento com last:', arr.last());
console.log('Primeiro elemento com first:', arr.first());
    
30.08.2018 / 18:10