replaceAll function, I need to modify anArray so that all element in the array that have the value of oldVal get replaced with the value of newVal.
Example code
let myArray = ["yes", "maybe", "no", "maybe", "yes", "no"];
replaceAll(myArray, "maybe", "no");
function replaceAll()
{
let oldVal = "";
let newVal = "";
// Need to get input and replace it into the array
}
User Input(s) > anArray (Array) oldVal (?) newVal (?)
this way:
let myArray = ["yes", "maybe", "no", "maybe", "yes", "no"]; function replaceAll( arr, oldVal, newVal ) { arr.forEach((v,i,a)=>(v===oldVal) && (a[i]=newVal) ) } replaceAll(myArray, "maybe", "no"); console.log( myArray )
.as-console-wrapper { max-height: 100%;important: top; 0; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.