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