Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Vuejs iterar a través de un objeto ref

Tengo un pequeño problema, obtengo mi objeto ref de ese método.

 const dataAnimals = ref([]) function getDataAnimals() { axios.get('/json/data_animal.json').then((response) => { dataAnimals.value = response.data }) } getDataAnimals()

Y quiero usar otro método usando ese objeto ref:

 function countAnimal(type) { dataAnimals.forEach((item) => { if (animal.animal == type) { total_hen += dataMint.value[animal.template_id] } return total_hen }) } const totalHen = countAnimal('hen')

Pero no puedo iterar a través de:

 dataAnimals.value.forEach((item) => {

¿Hay alguna forma de hacer que funcione?

Gracias :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Como la respuesta es un objeto y no una matriz, no puede iterar sobre ella con forEach , necesita usar Object.entries()

 function countAnimal(type) { let total = 0; for (const [key, item] of Object.entries(dataAnimals)) { if (item.animal === type) { total++; } } return total; } const totalHen = countAnimal('hen');

Y usaría un objeto reactivo:

 const dataAnimals = ref(null); function getDataAnimals() { axios.get('/json/data_animal.json').then((response) => { dataAnimals.value = response.data }); } getDataAnimals()

Por supuesto, si desea que ese recuento también sea reactivo, deberá usar una propiedad calculada.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!