Assuming you have a list of texts: (e.g.)
let myList = [
'item 1',
'item 2',
'item 3',
'item 4'
]
etc.
Then you can use the following:
function updateList(originalValue, newValue) {
let index = myList.findIndex(item => item === originalValue);
myList[index] = newValue;
}
And then invoke the function, with the intended parameters e.g.
updateList('item 2', 'item 406');
To change ‘item 2’ to ‘item 406’
you probably also want to include a check to make sure the item to change actually exists, to avoid any potential issues… such as:
function updateList(originalValue, newValue) {
let index = myList.findIndex(item => item === originalValue);
if (index >= 0) {
myList[index] = newValue;
} else {
console.log(`Item ${originalValue} not found`);
}
}
2 Likes