I am attempting to replace all of the instances of a defined string within my array with another, different defined string. Think, replacing all empty strings with 'n/a' or something similar.
function replaceThis(array, replace, withThis) {
const mapped = array.map(i => {
if (i === replace) {
array[array.indexOf(i)] = withThis;
}
})
return mapped
}
However, when I run this, I seem to get an array of all undefined items.
Am I missing something?
function x(array, replace, withThis) {
console.log(array);
const m = array.map(i => {
if (i === replace) {
array[array.indexOf(i)] = withThis;
}
})
console.log(m);
return m;
}
x('one', '', 'three');
.map()expects a return value and you are passing string and not an array