描述
该函数接受两个参数分别为数组、索引值,要求在不改变原数组的情况下返回删除了索引项的新数组。
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
const _delete = (array,index) => {
//1、要求不改变原数组,所以可以先将原数组拷贝一份,array.concat()
let newarr = array.concat();
//2、用splice()截取元素,此函数会改变数组
newarr.splice(index,1);
return newarr;
}
//方法二:JSON
const _delete = (array,index) =>{
let newArr = JSON.parse(JSON.stringify(array))
newArr.splice(index, 1)
return newArr
}
</script>
</body>
</html>