Remove array from array array

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 want 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?

    
asked by anonymous 25.11.2014 / 18:33

2 answers

3

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

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

Furthermore, the fc(el) function can 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. So if we assume that the array elements are always strings (for example), it is safe to reduce the arrays equality condition 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 compressed (which I did not do, for didactic purposes).

To finish, chain filters is simple (just apply a new filter() on the result of the filter() previous), 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 elements that coincide with certain other elements. So I suggest you create an array of elements to exclude:

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);
    
25.11.2014 / 19:20
-1

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

    
28.09.2018 / 13:24