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

155
Vistas
array.filter returns empty array in vue3

I have a simple filter operation after delete which for some reason returns an empty array. The values are evaluated correctly, but allSheep ref gets reassigned to an empty array after the operation is done. I am not sure why this is happening. Here is my setup function

  setup() {
    const allSheep = ref([]);
    const formOpen = ref(false);
    const searchQuery = ref("");
    const searchTag = ref("tag_id");
    const axios = inject("axios");
    axios
      .post(GRAPHQL_API_URL, {
        query: print(ALL_SHEEP),
      })
      .then((response) => {
        allSheep.value = response.data.data.get_all_sheep;
      })
      .catch((err) => console.log(err));

    const filteredSheep = computed(function () {
      const query = searchQuery.value.toLowerCase();
      return searchTag.value === "breed"
        ? allSheep.value.filter((el) => el.breed.breed_name.includes(query))
        : allSheep.value.filter((el) =>
            el[searchTag.value].toLowerCase().includes(query)
          );
    });

    function getSearchQuery(query) {
      searchQuery.value = query;
    }

    function setTag(key) {
      searchTag.value = key === "tag" ? (key = "tag_id") : key;
      searchQuery.value = "";
    }

    function handleOpenForm() {
      formOpen.value = true;
    }
    function updateData(data) {
      allSheep.value = [...allSheep.value, data];
    }

    function deleteSheep(id) {
      allSheep.value = allSheep.value.filter((el) => {
        el.sheep_id !== id;
      });
    }

    return {
      allSheep: filteredSheep,
      getSearchQuery,
      setTag,
      handleOpenForm,
      updateData,
      formOpen,
      searchQuery,
      searchTag,
      deleteSheep,
    };
  },
};
</script>

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

0

All i needed to do was replace curly braces with parentheses:

    function deleteSheep(id) {
          allSheep.value = allSheep.value.filter((el) => (
            el.sheep_id !== id;
          )
         );
        }
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