Remove a specific element from the javascript array

6

I need to remove an element from the array by index, for example:

arr = [1,2,3,4,5,6]

When removing the index element 3:

arr = [1,2,3]

It needs to remove everything after the index entered including the index.

I tried to do this:

    var localUltimo = w_history.length - 1;
    var anterior = w_history[localUltimo];
    // var ultimo = w_history.pop();
    for(var i = 0; i < w_history.length; i++){
        console.log("tam " + w_history[i] + " ultm " + anterior);
        if(w_history[i] == anterior){
            w_fim = i;
            break;
        }
    }

    console.log("w_fim " + w_fim);
    console.log("tamanho " + w_history.length);

    while(w_history.lenght > w_fim){
        console.log("w_fim " + w_fim);
        console.log("tamanho " + w_history.length);
        w_history.pop();
    }
    var ref = w_history[localUltimo - 1];
    console.log("ref " + ref)
    activate_page(ref, '1');
    w_history.slice(0, ref)
    console.log(w_history);

But the result was not satisfactory when running it adds a undefined element and immediately after cleaning every array:

ARRAY BEFORE:

["#login", "#listar_CELULAS", "#minha_CELULAS", #listar_CELULAS]

ARRAY AFTER:

["#login", "#listar_CELULAS", "#minha_CELULAS", "#listar_CELULAS", "#minha_CELULAS"]

Thank you.

    
asked by anonymous 13.01.2016 / 14:15

3 answers

6

As Pedro Camara replied you can use the slice method, however this method does not remove elements from the original array, but returns a new array with the elements specified by the start and end parameters. There is another method called splice that removes / adds elements in the original array and also returns the removed elements. In the splice method you specify the initial index and the amount of elements to be removed including the initial index element.

var test = [1, 2, 3, 4];
test.splice(2, 1);

[1] link

    
13.01.2016 / 15:38
5

You can use the slice method.

  • Parameters are the starting index and end index you want
  • The method returns a new array with the selected elements
  • The selected elements will be those that are in the initial index up to the final index, but the element in the final index will not be included
  • The original array will not be changed.

var arr = [1, 2, 3, 4, 5, 6];
var novoArr = arr.slice(2, 5);
console.log('Array original: ' + arr);
console.log('Novo array: ' + novoArr);
    
13.01.2016 / 14:20
3

As I explain in my video about Removing Vector Elements in JavaScript and also as other users have already replied, basically you can use the Array.splice(indice, quantidade) function passing as a parameter the índice from which you want to start removing vector elements and the quantidade of elements you want to remove.

After executing, the Array.splice function will modify the original vector by removing the requested elements and will also return a new vector containing the elements that have been removed.

Example :

var vetor = ["A", "B", "C", "D", "E"];
var elementosRemovidos = vetor.splice(1, 2); // Remove o segundo e terceiro elementos do vetor.

console.log(elementosRemovidos); // ["B", "C"]
console.log(vetor); // ["A", "D", "E"]
    
18.08.2017 / 13:41