爱生活,爱编程,学习使我快乐
// 方法一:常规方法
var arr = [ 1,2,2,4,2 ];
for ( var i=0; i<arr.length; i++ ) {
for ( var j=i+1; j<arr.length; j++ ) {
if ( arr[i] == arr[j] ) {
arr.splice( j, 1 );
j--;
}
}
}
alert( arr );
// 方法二:Set
var arr = [ 1,2,2,4,2 ];
alert([...new Set(arr)])
var str = 'assssjdssskssalsssdkjsssdss';
var arr = str.split('');
str = arr.sort().join('');
var value = '';
var index = 0;
var re = /(\w)\1+/g;
str.replace(re,function(0,1){
if(index<0.length){
index =0.length;
value = $1;
}
});
alert('最多的字符:'+value+',重复的次数:'+index);
var reg = /^[a-zA-Z].*\d$/;
var str = "s)*&)1";
reg.test(str);
str.split("").reverse().join("");
display:flex;
flex-direction:column;
justify-content:center;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
function fun(n,o){
console.log(o);
return {
fun:function(m){//[2]
return fun(m,n);//[1]
}
}
}
var a=fun(0);
a.fun(1);
a.fun(2);
a.fun(3);
var b=fun(0).fun(1).fun(2).fun(3);
var c=fun(0).fun(1);
c.fun(2);
c.fun(3);
为了更好理解,等价转换为
function _fun_(n,o){
console.log(o);
return {
fun:function(m){
return _fun_(m,n);
}
}
}
var a=_fun_(0);//undefined
a.fun(1);//0
a.fun(2);//0
a.fun(3);//0
var b=_fun_(0).fun(1).fun(2).fun(3);
//undefined,0,1,2
var c=_fun_(0).fun(1);//undefined,0,
c.fun(2);//1
c.fun(3); //1