Skip to content

Instantly share code, notes, and snippets.

@ancyrweb
Created January 12, 2022 07:10
Show Gist options
  • Save ancyrweb/d2a7510db2ca9bd06f1986a835c80518 to your computer and use it in GitHub Desktop.
Save ancyrweb/d2a7510db2ca9bd06f1986a835c80518 to your computer and use it in GitHub Desktop.
const elements = [1, 2, 4, 5];
const indexToInsert = 2; // we will append the element at index 2
const elementToInsert = 3; // we will insert the number "3"
const nextElements = elements.reduce((previousValue, currentValue, index) => {
previousValue.push(currentValue); // 😱
if (index === indexToInsert) {
previousValue.push(elementToInsert); // 😱😱
}
return previousValue;
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment