Let's assume that we want to replace an item in an array in JavaScript. For example, we want to replace Erik
with Erik Martín
:
javascript// Declaring array let arr = ['Carlos', 'Erik', 'Luis', 'Victoria', 'Jorge', 'Erik']; // Replace element using map let replaced = arr.map(name => name === 'Erik' ? 'Erik Martín' : name); console.log(replaced); // ['Carlos', 'Erik Martín', 'Luis', 'Victoria', 'Jorge', 'Erik Martín']
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.