Remove array from an array

Asked

Viewed 1,298 times

1

I have the following data in an array

var array = [
             ["755", "20", "E", "274", $$hashKey: "0AK"],
             ["756", "20", "E", "274", $$hashKey: "0B7"],
             ["455", "30", "E", "159", $$hashKey: "0BQ"],
             ["757", "20", "E", "274", $$hashKey: "09X"],
             ["456", "30", "E", "159", $$hashKey: "0CB"],
             ["269", "20", "E", "160", $$hashKey: "0CM"],
            ];

I wish to remove from this array only the array that is equal to:

var = item = ["757", "20", "E", "274"]

How do I remove only this array?

  • 1

    This array is not a valid JS object. O $$hashKey: "xxx" is an object?

  • This $$hashKey is the return of the Angularjs used in ng-repeat.

2 answers

3


You can use the function filter(fc), whose parameter fc must be a function that takes each element of the array as argument (in other words, a function fc(el)); in your case, each element of the original array is another array.

The function fc(el) has the purpose to say whether the element should be maintained (return value equivalent to true), or else excluded from the resulting array of the function filter (resulting value false).

Still, the function fc(el) may be anonymous, as in the example below:

var array = [
    ["755", "20", "E", "274"],
    ["756", "20", "E", "274"],
    ["455", "30", "E", "159"],
    ["757", "20", "E", "274"],
    ["456", "30", "E", "159"],
    ["269", "20", "E", "160"]
];

// Atenção à função "filter(fc)", cuja função-parâmetro "fc", nesse caso, é anônima:
var array_filtrado = array.filter(function(arr){

    // Para cada array filho ("arr"), verifica se ele é igual a ["757", "20", "E", "274"]:
    var igual = (arr[0] == "757") &&
                (arr[1] == "20" ) &&
                (arr[2] == "E"  ) &&
                (arr[3] == "274") ;

    // Se for igual, exclui do resultado (return false):
    return !igual;
});

console.log(array_filtrado); // Mostra o resultado no console do browser.

Obviously, it is always possible to improve a source code, especially if some kind of simplification can be used. Therefore, if we assume that the array elements are always strings (for example), it is safe to reduce the equal status of arrays to a simple a.toString() == b.toString():

var array = [
    ["755", "20", "E", "274"],
    ["756", "20", "E", "274"],
    ["455", "30", "E", "159"],
    ["757", "20", "E", "274"],
    ["456", "30", "E", "159"],
    ["269", "20", "E", "160"]
];
var array_filtrado = array.filter(function(arr){
    var igual = arr.toString() == ["757","20","E","274"].toString();
    return !igual;
});
console.log(array_filtrado);

What can be further compacted (thing I didn’t do, for didactic purposes).

To finish, chain filters is simple (just apply a new filter() on the outcome of filter() ), and can be done through two syntaxes:

// Sintaxe 01:
var array_filtrado = array.filter(...);
array_filtrado = array_filtrado.filter(...);
array_filtrado = array_filtrado.filter(...);

// Pode ser reduzido para: (Sintaxe 02)
var array_filtrado = array.filter(...).filter(...).filter(...) ///...

This, however, is too costly for your particular case, in which we always want to apply the same filtering criterion: exclude those elements that coincide with other certain elements. Therefore, I suggest you create an array of elements to be deleted:

var array = [
    ["755", "20", "E", "274"],
    ["756", "20", "E", "274"],
    ["455", "30", "E", "159"],
    ["757", "20", "E", "274"],
    ["456", "30", "E", "159"],
    ["269", "20", "E", "160"]
];
var excluir = [
    ["757", "20", "E", "274"],
    ["269", "20", "E", "160"]
];
var array_filtrado = array.filter(function(arr){
    var igual = false;
    for(var i = 0; i < excluir.length; i++){
        if(arr.toString() == excluir[i].toString()) igual = true;
    }
    return !igual;
});
console.log(array_filtrado);

  • It would work in parts, problem that if I remove this array ["757","20","E","274"] and remove another array, the first one appears again

  • Examples show how to use the function filter(), and give a trick to a simpler equality condition; you can increment them, for example: chaining exclusions, as you want. Just apply the second filter over the outworking of the first, that is, on the array_filtrado :D

  • Anyway, I incremented the answer with one more example, which will solve your problem without having to chain filters (which, by the way, would make the maintenance of the code a nightmare!).

-1

You can use the delete (array[valor do indice do array que quer apagar]);

Browser other questions tagged

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