Remove a specific element from the javascript array

Asked

Viewed 61,753 times

9

Well I’m needing to remove an element from the array by index, for example:

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

When removing index element 3:

arr = [1,2,3]

It is necessary that he remove everything after the reported index including the index.

I tried to do it this way:

    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 executing it adds an element undefined and then clean up the whole array:

ARRAY BEFORE:

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

ARRAY LATER:

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

Thank you.

  • You want to remove index element 3 and all later?

  • Exactly, this is what I need

  • It would be complicated by the actual array, before and after?

  • I edited the question

  • Sorry Renan still not understood what you want, what I understood is that you remove the first element and concatenated the array with the own array without the first element

3 answers

9


As Pedro Camara answered 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 Dice and the amount of elements to be removed including the initial Dice element.

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

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

7

You can use the method Slice.

  • The parameters are the initial index and the final 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 until 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);

  • 1

    I was just about to comment on that

  • It’s not working for me, it just adds a Undefined >["#login", "#listar_CELULAS", "#minha_CELULAS", Undefined, Undefined] ionic_subpage.js:39 ["#login", "#listar_CELULAS", "#minha_CELULAS", Undefined, Undefined]

  • Getting to solve my problem, only that it’s not cutting is just adding

  • What did you mean by just cutting @Renanrodrigues? I edited the answer by assigning the new array to the previous one.

5

As I explain in my video about How to Remove Vector Elements in Javascript and also as other users already answered, basically you can use the function Array.splice(indice, quantidade) passing as parameter the índice from which you want to start the removal of vector elements and the quantidade of elements you wish to remove.

After being executed, the function Array.splice 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"]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.