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

139
Views
How can I modify every other element in an array using JS?

So the data is like this:

let array = ["Yes", "Yes", "Yes", "Yes"];

Expected Result:

array = ["Yes", "", "Yes", ""];

I've seen some answers on Stackoverflow, but I haven't found one that shows how to modify every other element. Most show how to filter, like this one:

let x = arrar.filter((element, index) => {
  return index % 2 === 0;
})

Appreciate your help!

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

0

As Emiel pointed out in the comments, you don't want to use .filter() here as that would reduce your original array. Instead use .map():

let array = ["Yes", "Yes", "Yes", "Yes"];

let x = array.map((element, index) => {
  return (index % 2 === 0) ? element : '';
})

console.log(x)

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!