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);
This array is not a valid JS object. O
$$hashKey: "xxx"
is an object?– carlosfigueira
This $$hashKey is the return of the Angularjs used in ng-repeat.
– PGz