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

140
Views
Array not resetting at end of loop

I have the code below that takes the subKeyValuesList and removes values from it as the loop iterates. This works however after each loop (it's one big array containing 4 arrays) I want the subKeyValuesList values to reset to the original value. Below I've tried to reset by applying some code to the last iteration of the loops but no joy. I think it may be a scope issue but have struggled to see where the issue is

 var subKeyValuesList = ['0009397b-563f-4dfc-8022-1516dbdffbda','1358d485-69e6-43bb-bc2f-ff25baa6a810']

   for (var fi = 0; fi < fields.length; fi++) {
       var subKeyValuesList2 = subKeyValuesList
       var fieldSplitTemp = fieldLong.replace(/[`~!@#$%^&*()_|+\=?;:'"<>\{\}\[\]\\\/]/gi, 'x')
       var fieldSplit = fieldSplitTemp.split("xix.")
       var fieldCount = fieldSplit.length
                                        
    var arrayPath = fieldSplit.splice(0, fieldCount - 1)

    for (let i = 0; i < eval(arrayPath[0]).length; i++) {
                
     for (let ii = 0; ii < eval(arrayPath[0] + "[i]." + arrayPath[1]).length; ii++) {
          var keyNames = Object.keys(eval(arrayPath[0] + "[i]." + arrayPath[1] + "[ii]"));
          var subKeyName = keyNames[0]
          var parent = eval(arrayPath[0] + "[i]." + arrayPath[1] + "[ii]." + subKeyName)
                            
         var index = subKeyValuesList2.indexOf(parent);
    
           if (index !== -1) {                                    
                                subKeyValuesList2.splice(index, 1)
                            }           
           if (ii == eval(arrayPath[0] + "[i]." + arrayPath[1]).length - 1) {
        //if last instance in loop do reset spliced list with original subKeyValuesList
                    subKeyValuesList2 = subKeyValuesList
                        }

                    }

            }
        }   
about 4 years ago · Juan Pablo Isaza
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!