<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<ol>
<li>招商银行-招银网络科技</li>
<li>https://cmbnt.cmbchina.com/</li>
<ol>
<ul>
<li>招商银行-招银网络科技</li>
<li>https://cmbnt.cmbchina.com/</li>
<ul>
</body>
<script>
var arr = [1,2,2,22,4,5,5,5]
//数组去重最简单方法
var newArray = new Set(arr)
console.log('---',[...newArray]);
// map
// map()方法定义在JavaScript的Array中,
// 它返回一个新的数组,新返回数组中的每个元素为原始数组的每一个元素分别调用map中的回调函数处理后的值。
// array.map(function(currentValue, index, arr), thisIndex)
console.log(arr.map((cur,index)=>{
if(cur%2==1) //奇数
return cur+2000
return cur
}));
// reduce
// 对数组中的所有元素调用指定的回调函数。
// 该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
// array.reduce(function(pre, currentValue, currentIndex, arr), initialValue)
// 1、previousValue (上一次调用回调返回的值,或者是提供的初始值(initialValue))
// 2、currentValue (数组中当前被处理的元素)
// 3、index (当前元素在数组中的索引)
// 4、array (调用 reduce 的数组)
// initialValue (作为第一次调用 callback 的第一个参数。)
var t = arr.reduce((pre,cur,index)=>{
pre[index] = cur
return pre
},{})
console.log(t);
//filter
// filter为数组中的每个元素都执行一次callback函数,
// 并且将能够使callback函数返回true或者与true等价的值的元素组合成一个新的数组
// filter用法:Arrary.filter(callback(element,[index],[array]),[thisArg])
// 参数:
// callback:数组元素执行的回调函数
// element:当前正在处理的元素
// index:当前正在处理的元素的索引值
// array:调用 filter 方法的数组
// thisArg:执行callback时,用于this的值
var filter = arr.filter((cur,index)=>{
if(cur%2==1) return true
})
console.log(filter);
</script>
</html>
js数组去重最简单的方法&js数组map,reduce,filter的使用介绍
最新推荐文章于 2024-04-09 02:39:02 发布