原文:https://blog.csdn.net/funche/article/details/106308936
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script>
// 原文:https://blog.csdn.net/funche/article/details/106308936
var arr = [
{
id: 0,
name: 'Tom0'
}, {
id: 1,
name: 'Tom1'
}, {
id: 2,
name: 'Tom2'
}, {
id: 3,
name: 'Tom3'
}, {
id: 4,
name: 'Tom4'
}
]
// 把 Tom0 跟 Tom3 位置对调
var sourceIndex = 0
var targetIndex = 3
arr[sourceIndex] = arr.splice(targetIndex, 1, arr[sourceIndex])[0]
console.log(arr);
</script>
</body>
</html>