ES6 replace element in array in typescript

what is the clean way to replace element from a array in es6

function findReplace(item, target, replacement){
  return item === target ? replacement : item
}
const a = [1,2,3,4,5,5,4,3,2,1]
console.log(a.map(item => findReplace(item, 1, 10)).map(item => findReplace(item, 2, 20)))
//[10,20,3,4,5,5,4,3,20,10]

Answer

Create a map for the translation:

const a = [1,2,3,4,5,5,4,3,2,1]
const map = new Map([[1, 10], [2, 20]]); 

console.log(a.map(item => map.get(item)??item))
//[10,20,3,4,5,5,4,3,20,10]

If you want to replace values with null or even undefined, then the ?? operator is not the right tool. In that case:

const a = [1,2,3,4,5,5,4,3,2,1]
const map = new Map([[1, null], [2, undefined], [3, 33]]); 

console.log(a.map(item => map.has(item) ? map.get(item) : item))