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

123
Vistas
How to run a function twice and save output for comparison?

I have created a function that compares two arrays and makes sure they are not equal to each other. I run the function twice but I must admit it is a very poor design, however, it was the only way I could get it to work. Is there an easier way to accomplish the same thing? Maybe deepcloning with lodash? Here is my go at it.

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
}

Is there an easier way using Javascript to do the same thing without counting the number of times the function is called and running the same thing again?

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

0

Return the array each time and compare at the end of the sequence,

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);
}

I am wondering how chart gets changed - is there some cy.get() commands involved?

If so you will have to return the result of them and nest the results

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 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