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

106
Views
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 answers
Answer question

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