<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
<script>
//以下代码我们使用 JS 的 splice() 方法来移除数组的指定元素:
const array = [2, 5, 9];
document.write(array);
const index = array.indexOf(5); // 移除元素5
if (index > -1) {
// 移除找到的指定元素
array.splice(index, 1); // 移除元素
}
document.write("<br>");
document.write(array);
// array = [2, 9]//页面显示==>>2, 9
</script>
</html>
运行结果