<!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>
<script>
let arr=[1,2,3,4,5,6,4,3,2,1]
arr=arr.reduce(function(pre,cur){
if(pre.indexOf(cur)==-1){
pre.push(cur)
}
return pre //重要
},[])
console.log(arr)
</script>
</body>
</html>
reduce有两个参数时,开始的pre为第二个参数,我这里是空数组,即开始pre为空数组.
执行过程中,pre为返回的值,cur为数组的元素往后遍历