How to remove the position of an array with jquery

1

I'm trying to remove a position from the array with slice, from what I saw until I can only get the position of this array, how would I get value? Because the position of the array changes a lot in each situation, I'm trying this way:

if($j('#folog_manha').css('display') == "none"){
   var retornox = retorno.slice('Manhã');
}

If it does not, how would I get the positions of this array to compare? in case you take the position that is 'Morning' and give a slice in it.

    
asked by anonymous 30.01.2018 / 13:18

2 answers

2

To remove a position from an array by value just use IndexOf in the splice

var arr = [ "abc", "def", "ghi" ];

arr.splice(arr.indexOf("def"), 1);
    
30.01.2018 / 13:28
4

Try to do as in this example:

var arr        = ['a', 'b', 'c', 'd', 'e']; //Array inicial
var removeItem = 'c';   // Valor do array que será removido

arr = jQuery.grep(arr, function(value) {
    return value != removeItem;
});
console.log('Array após a remoção: ' + arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Inyourcodeitwouldlooklikethis:

if($j('#folog_manha').css('display')=="none"){
   var removeItem = 'Manhã';

   retorno= jQuery.grep(retorno, function(value) {
    return value != removeItem;
   });

   // new array
   // fica sem "manha"

}

jQuery's grep method traverses all indexes in an array and returns the ones you want.

Reference link

    
30.01.2018 / 13:24