배열간의 요소 swap

김태완·2023년 1월 4일
0

구조분해할당을 응용하여 array의 요소를 swap해주는 간단한 함수이다.

const swapArrayItemsByIdx = (arr: any[], index1: number, index2: number) => {
    [arr[index1], arr[index2]] = [arr[index2], arr[index1]];
};
profile
프론트엔드개발

0개의 댓글