一个简单的用JS计算重复个数的方法(只适合数组中只有一个元素有多个重复的情况)

<!DOCTYPE html >
< html lang= "en" >
< head >
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< meta http-equiv= "X-UA-Compatible" content= "ie=edge" >
< title >Document </ title >
</ head >
< body >
</ body >
< script >
// 1首先是需要查重的元素
var arr= "weqer";
// 2将需要查重的数组进行分割
var b= arr. split( "");
// 3定义一个空数组用来接收过滤后的数组
var d=[];
   // 4数组的去重操作
for ( var i = 0; i < b. length; i++) {
if( d. indexOf( b[ i])==- 1){
d. push( b[ i]);
}
}
// 5将原数组长度跟过滤后数组长度进行相减得到加1得到重复的个数
var e= b. length- d. length+ 1;
console. log( e);
< / script >
</ html >

猜你喜欢

转载自blog.csdn.net/weixin_41905935/article/details/79917873