Skip to content

Instantly share code, notes, and snippets.

@jagomf
Created June 4, 2018 14:34
Show Gist options
  • Save jagomf/3d49ffc13314da0977c2803696ba822f to your computer and use it in GitHub Desktop.
Save jagomf/3d49ffc13314da0977c2803696ba822f to your computer and use it in GitHub Desktop.
Replace element in array using Object.assign
const arr = ['a', 'b', 'c', 'd', 'e'];
const postToReplace = 2; // position 2 points to letter 'c' in the array
const newArr = Object.assign([...arr], {[postToReplace]: 'J'});
console.log(newArr);
// ['a', 'b', 'J', 'd', 'e']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment