How can I get the "e"
elements inside of "arr" to be replaced by change0
?
The arr
array will be an input by the user and I need to change it there is no way to predict which element will be "e"
.
var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"];
var change0 = 2
var change1 = 1
document.write(arr);
You could use map()
method and this will return new updated array and save original.
var arr = [ "a", "b", "c", "d", "e", "f", "g", "h", "e", "j", "e"];
var change0 = 2;
var result = arr.map(e => e == 'e' ? change0 : e);
console.log(result)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With