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

109
Vistas
Clearing an array after sending to another function as an argument

I am struggling to figure out how to make this work. I thought if I sent an array as an argument to another function I'd be able to clear it but every time I do I get errors from the function it was sent to. I am trying to filter two 2D arrays filled with coordinates {x:, y:}. Everything works well except I can't clear val after each time I pass it to my check() function. If I do attempt to clear it i get errors from within my check() function.

I wouldn't expect it to as I thought once it was sent as an argument it wouldn't matter any more. I've also tried creating a copy in multiple ways but no matter what, by splicing val or doing val = 0, the check function, specifically when calling length(validate[0]) it throws an error.

I've been at it to long now. Any help is appreciated.

    let val = [];
    let hLines = []
    function compare() {
      for (let i = 0; i < 8; i++) {
        //val = []
        for (let j = 0; j < sortY[i].length; j++) {
          for (let k = 0; k < sortX.length; k++) {
            for (let l = 0; l < sortX[k].length; l++) {
              if (sortY[i][j] == sortX[k][l] && !val.includes(sortX[k])) {
                val.push(sortX[k])
              }
            }
          }
          if (j === sortY[i].length - 1) {
            //let copy = val.slice()
            //check(sortY[i], copy)
            check(sortY[i], val)
            //val = []
          }
        }
      }
    }
    compare()

    
    function check(hLine, validate) {
      for (let i = 0; i < validate.length; i++) {
        for (let j = 0; j < validate[i].length; j++) {
          let first = validate[i][0];
          let last = validate[i][validate[i].length - 1];

          if (validate[i][j] != first && validate[i][j] != last) {
            validate[i].splice(j, 1)
            j--
          }
        }
      }

      let lengthValue = length(validate[0])
      let lengthCheck = validate.every((v, i) => {
        return length(v) === lengthValue
      })

      if (!lengthCheck) {
        hLines.push(hLine)
      }
    }
    console.log(hLines)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

To create a referenceless copy use this function:

function copy(aObject) { // Deep Clone Object from https://stackoverflow.com/a/34624648/16642626
    if (!aObject) {
        return aObject;
    }

    let v;
    let bObject = Array.isArray(aObject) ? [] : {};
    for (const k in aObject) {
        v = aObject[k];
        bObject[k] = (typeof v === "object") ? copy(v) : v;
    }

    return bObject;
}

In case you are wondering: the reference is only lost when copying primitive datatypes, thus you need to go through an objects layers and copy it from the base layer.

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