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

185
Views
how insert an index into an array in a loop js

I want to make two tables to identify the position of each image before an animation. I tried several solutions I think the most logical would be a .push() but it doesn't work. Here is a code snippet:

const img = document.querySelectorAll('.myClass img')
let fromRight = []
let fromLeft = []
for (let i = 0; i < img.length; i++) {
  document.addEventListener('load', ()=>{
    if (img[i].getBoundingClientRect().x < (window.scrollY/2)) {
      fromLeft.push(i)
    } 
    else if (img[i].getBoundingClientRect().x > (window.scrollY/2)) {
      fromRight.push(i)
    }
  })
  console.log('left :', fromLeft);
  console.log('right :', fromRight);
}

I also tried:

Array.prototype.push.apply(fromLeft, i)

And :

fromLeft.concat(i)

Please I need help!

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

0

Try wrapping the whole code inside a single listener

document.addEventListener('load', ()=>{
  const img = document.querySelectorAll('.myClass img')
  let fromRight = []
  let fromLeft = []
  for (let i = 0; i < img.length; i++) {
    if (img[i].getBoundingClientRect().x < (window.scrollY/2)) {
      fromLeft.push(i)
    } 
    else if (img[i].getBoundingClientRect().x > (window.scrollY/2)) {
      fromRight.push(i)
    }
  }
  console.log('left :', fromLeft);
  console.log('right :', fromRight);
})

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!