配列の要素入れ替え

created 2021/08/15updated 2021/08/15

TLDR

js
[arr[0], arr[1]] = [arr[1], arr[0]]

Example

js
let arr = [ { id: 1, name: 'Number1' }, { id: 2, name: 'Number2' }, ]; [arr[0], arr[1]] = [arr[1], arr[0]] console.log(arr) // 0: {id: 2, name: "Number2"} // 1: {id: 1, name: "Number1"}

Usecase

swap