JS常用内容

it2022-05-05  174

原生

获取当前日期的字符型

function getCurrentDate(){ var date= new Date(); var year = 0; var month = 0; var day = 0; var currentDate = ""; //初始化时间 year = date.getFullYear();//ie火狐下都可以 month = date.getMonth() + 1;//国外是0-11代表12个月; day = date.getDate(); currentDate = year + "-"; if(month >= 10){ currentDate += month + "-"; }else{ currentDate += "0" + month + "-"; } if(day >= 10){ currentDate += day; }else{ currentDate += "0" + day; } return currentDate; } console.log(getCurrentDate());//测试

遍历数组

//初始化一个字符数组 var stringArray = new Array(5); stringArray[0] = "I"; stringArray[1] = "am"; stringArray[2] = "doctorToliet"; stringArray[3] = "from"; stringArray[4] = "HeBei province"; var sentence = ""; //遍历数组 for(var index in stringArray){ sentence += stringArray[index] + " "; } console.log(sentence);

遍历Map键值对

//初始化Map var featureMap = {}; featureMap["name"] = "doctorToliet"; featureMap["home"] = "QianAn city HeBei province"; featureMap["age"] = 30; featureMap["hobby"] = "e-sports,hip-hop"; var introduceMyself = "Hello guys!"; //遍历Map键值对 for(var key in featureMap){ introduceMyself += "my " + key + " is " + featureMap[key] + ";\n"; } console.log(introduceMyself);

最新回复(0)