Created
January 12, 2022 07:10
-
-
Save ancyrweb/d2a7510db2ca9bd06f1986a835c80518 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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