统计字符串中各个字符串出现的次数 function repeatCount(str) { return str.split('').reduce((p, k) = (p[k]++ || (p[k] = 1), p), {});}var str = 'abcdaabc';var info = repeatCount(str);console.log(info); //{ a: 3, b: 2, c: 2, d: 1
function repeatCount(str) {
return str.split('').reduce((p, k) => (p[k]++ || (p[k] = 1), p), {});
}
var str = 'abcdaabc';
var info = repeatCount(str);
console.log(info); //{ a: 3, b: 2, c: 2, d: 1 }
阻止事件冒泡
function stopBubble(e){
if (e && e.stopPropagation)
e.stopPropagation()
else
window.event.cancelBubble=true
}
封装log函数
// 初步封装
function log(){
console.log.apply(console,arguments)
}
// 第二次封装,怎么才能在参数前面加前缀
function log(){
var newArguments = []
if(arguments.length > 0){
for(var i = 0; i < arguments.length; i++){
newArguments.push('prefix',arguments[i])
}
}
console.log.apply(console,newArguments)
}
数组的去重
function deletMany(arr){
var obj = {}
var newArr = []
for(var i=0; i
金额格式化
function toThousands(num) {
var potStr = '.00'
num = (num||0).toString()
if(num.indexOf('.') !== -1){
potStr = num.substr(num.indexOf('.'),3)
}
var result = [ ], counter = 0;
num = num.substring(0,num.indexOf('.')).split('');
for (var i = num.length - 1; i >= 0; i--) {
counter++;
result.unshift(num[i]);
if (!(counter % 3) && i != 0) { result.unshift(','); }
}
return result.join('')+potStr;
}
