Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
How can we update or refresh/renew filtered array?-javascript

updating filtered array every time original array adds/inputs a new element


so every time I input a new element in the original array, then the filtered array gets refreshed and considers if the new element is in its condition

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What about this, i am overriding the push method of the original array, so that when an new element is added, another method is going to get called to refresh the filtered array:

var originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var filteredArray = [...originalArray];
var filters = [];

// FUNCION THAT RECEIVES AN ARRAY AND OVERRIDES THE PUSH METHOD 
//SO THAT IT CAN EXECUTE A FUNCION AFTER PUSHING THE NEW ELEMENT
var AddEventToArrayPush = (arr, callback) => {
    arr.push = function(e) {
        Array.prototype.push.call(arr, e);
        callback(arr);
    };
};

var ResetFilters = () => {
    filters = [];
    filteredArray = [...originalArray];
}

var AddFilter = (filter) => {
    filters.push(filter);
}

var ApplyFilters = () => {
    filteredArray = [...originalArray];

    for (const filter of filters) {
        filteredArray = filteredArray.filter(filter);
    }
}

//CALLING THE FUNCTION TO OVERRIDE PUSH METHOD FOR THE ORIGINAL ARRAY
AddEventToArrayPush(originalArray, ApplyFilters)

// SOME FILTERS
const IS_EVEN_NUMBER = (number) => { return number % 2 == 0 };
const IS_GREATER_THEN_5 = (number) => { return number > 5 };

console.log("Initial filtered array:");
console.log(filteredArray.toString());

console.log("\r\nFiltering even numbers greater than 5");
AddFilter(IS_EVEN_NUMBER);
AddFilter(IS_GREATER_THEN_5);
ApplyFilters();

console.log("\r\nFiltered array after applying filters:");
console.log(filteredArray.toString());

originalArray.push(20);

console.log("\r\nFiltered array after pushing new element into original array:");
console.log(filteredArray.toString());
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda