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

121
Views
¿Cómo ejecutar una función dos veces y guardar la salida para comparar?

Creé una función que compara dos matrices y se asegura de que no sean iguales entre sí. Ejecuté la función dos veces, pero debo admitir que es un diseño muy pobre, sin embargo, era la única forma en que podía hacer que funcionara. ¿Hay una manera más fácil de lograr lo mismo? ¿Quizás clonación profunda con lodash? Aquí está mi intento.

 let callCount = 0; let values = []; let values2 = []; const getData = () => { const chart = [{ //dynamic: changes the id numbers on second call items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }, { id: 3, name: 'bar' }, { id: 4, name: 'bar' }] }]; if (callCount < 1) { //if and for block cannot be extracted for various reasons for (let i = 0; i < 5; i++) { const arr = chart[i].items[0]; arr.forEach((element) => { values.push(element.id); }); } callCount++; return values; } if (callCount >= 1) { for (let i = 0; i < 5; i++) { const arr = chart[i].items[0]; arr.forEach((element) => { values2.push(element.id); }); } expect(values).to.not.eq(values2); } }; it ('Comparing two arrays', function () { getData() // logic that will change effectively change some of the values of `chart` getData() // is successful but wildly inefficient }

¿Hay alguna manera más fácil de usar Javascript para hacer lo mismo sin contar la cantidad de veces que se llama a la función y se vuelve a ejecutar lo mismo?

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

0

Devuelve la matriz cada vez y compara al final de la secuencia,

 const getData = () => { const chart = [{ //dynamic: changes the id numbers on second call items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }, { id: 3, name: 'bar' }, { id: 4, name: 'bar' }] }]; const arr = chart[i].items[0].map(element => element.id) return arr; }; it ('Comparing two arrays', function () { const values1 = getData() // logic that will change effectively change some of the values of `chart` const values2 = getData() expect(values1).to.not.eq(values2); }

Me pregunto cómo se cambia el chart : ¿hay algún cy.get() involucrado?

Si es así tendrás que devolver el resultado de ellos y anidar los resultados

 it ('Comparing two arrays', function () { getData().then(values1 => { // logic that will change effectively change some of the values of `chart` getData().then(const values2 => { expect(values1).to.not.eq(values2); }) }) }
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!