获取2017-06-10格式的日期
function formateDate(dt){
if(!dt){
dt = new date();
}
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var date = dt.getDate();
if(month<10){
month = "0" + month;
}
if(date<10){
date = "0" + date;
}
return year + "-" + month + "-" + date;
}
var dt = new Date();
var formateDate = formateDate(dt);
console.log(formateDate); // 2017-12-08
获取随机数,要求是长度一致的字符串格式
var random = Math.random();
random = random + '0000000000'; // 后面加上10个0
random = random.slice(0,10);
console.log(random); // 0.95975987
写一个能遍历对象和数组的forEach函数
function forEach(obj,fn){
if(obj instanceof Array){
obj.forEach((item,index) => {
fn(index,item);
});
}else{
for(key in obj){
if(obj.hasOwnProperty(key)){
fn(key,obj[key]);
}
}
}
}
var arr = ['a','b','c'];
forEach(arr,(index,item)=>{
console.log(index,item);
});
// 0 'a'
// 1 'b'
// 2 'c'
var obj = {x:100,y:200};
forEach(obj,(index,item)=>{
console.log(index,item);
});
// x 100
// y 200