I would like to change a nested array that contains a * to all the same * values . For instance, this grid
["A", "*", "C"],
["D", "E", "p"],
["G", "H", "I"],
Would turn into
["*", "*", "*"],
["D", "E", "p"],
["G", "H", "I"],
Here is my function so far:
function changeArr(grid) {
for (let arr of grid) {
if (arr.includes("*")) {
arr.forEach(element => element = "*")
}
}
return grid;
}
My function just returns the same initial grid that was passed through the function. I've also tried
function changeArr(grid) {
for (let arr of grid) {
if (arr.includes("*")) {
arr.splice(0, 3, ["*","*","*"])
}
}
return grid;
}
But this creates a nested array within a nested array! I feel like this can be easily accomplished with a forEach but not sure how.
splice()should work after you remove the brackets, soarr.splice(0, 3, "*", "*", "*");remains.