查找字符串出现的次数
function countStr(str,strSplit){
return str.split(strSplit).length-1
}
var a="helloworldHellonihaogreetinghello";
console.log(countStr(a,"hello"));
数组去重
摘抄:
ES6新增的Set数据结构,类似于数组,但是里面的元素都是唯一的 ,其构造函数可以接受一个数组作为参数
let arr=[1,2,1,2,6,3,5,69,66,7,2,1,4,3,6,8,9663,8]
let set = new Set(array);
{1,2,6,3,5,69,66,7,4,8,9663}
ES6中Array新增了一个静态方法from,可以把类似数组的对象转换为数组
Array.from(set)
[1,2,6,3,5,69,66,7,4,8,9663]
function removeRepeatArray(arr){
return Array.from(new Set(arr))
}
console.log(removeRepeatArray([1,2,2,3,3,3,3,3,3,3,3]))
打乱数组
function resetArr(arr){
return arr.sort(function(){
return Math.random()-0.5
})
};
arr=[1,2,3,3,3,3,3,234,123,1234,1324,1234,5123,4123,4];
console.log(resetArr(arr));