<ul class="nav">
<li>榴莲</li>
<li>臭豆腐</li>
<li>鲱鱼罐头</li>
<li>大猪蹄子</li>
</ul>
<script>
function showTime() {
var nowdate = new Date();
var year = nowdate.getFullYear(),
month = nowdate.getMonth() + 1,
date = nowdate.getDate(),
day = nowdate.getDay(),
week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
h = nowdate.getHours(),
m = nowdate.getMinutes(),
s = nowdate.getSeconds(),
h = checkTime(h),
m = checkTime(m),
s = checkTime(s);
return year + "年" + month + "月" + date + "日" + week[day] + h + ":" + m + ":" + s;
};
function checkTime(i) {
return i < 10 ? '0' + i : i;
};
console.log(showTime());
function countTime(time) {
var nowTime = +new Date();
var inputTime = +new Date(time);
var times = (inputTime - nowTime) / 1000;
var d = parseInt(times / 60 / 60 / 24),
h = parseInt(times / 60 / 60 % 24),
m = parseInt(times / 60 % 60),
s = parseInt(times % 60),
d = checkTime(d),
h = checkTime(h),
m = checkTime(m),
s = checkTime(s);
return d + '天' + h + '时' + m + '分' + s + '秒';
}
function checkTime(i) {
return i < 10 ? '0' + i : i;
};
console.log(countTime('2022-6-7 23:30:00'));
function unique(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (newArr.indexOf(arr[i]) === -1) {
newArr.push(arr[i]);
}
}
console.log(newArr)
}
var arr = [1, 1, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6];
console.log(unique(arr));
function unique(arr) {
var newArr = [];
arr.map((v, i) => {
if (arr.indexOf(arr[i]) == i) {
newArr.push(arr[i]);
}
})
return newArr;
};
var arr = [115, 22, 22, 554, 95, 884, 56, 115, 22];
console.log(unique(arr));
function unique(arr) {
return Array.from(new Set(arr));
}
var arr = ['aa', 'bb', 'cc', 'cc', 'dd', 'dd', 'ee', 'ee']
console.log(unique(arr));
function getStr(str) {
var index = str.indexOf('o');
var num = 0;
while (index !== -1) {
console.log(index);
num++;
index = str.indexOf('o', index + 1);
}
return 'o出现了' + num + '次';
}
console.log(getStr('abcoefoxyozzopp'));
function getMaxStr(str) {
var o = {};
for (var i = 0; i < str.length; i++) {
var chars = str.charAt(i);
if (o[chars]) {
o[chars]++;
} else {
o[chars] = 1;
}
}
var max = 0;
var ch = '';
for (var k in o) {
if (o[k] > max) {
max = o[k];
ch = k;
}
}
return '出现最多的是' + ch + '它的次数为' + max;
}
console.log(getMaxStr('abcoefoxyozzopp'));
var lis = document.querySelector('.nav').querySelectorAll('li');
for (var i = 0; i < lis.length; i++) {
lis[i].index = i;
lis[i].onclick = function () {
console.log(this.index);
}
}
for (var i = 0; i < lis.length; i++) {
(function (i) {
lis[i].onclick = function () {
console.log(i);
}
})(i);
}
var obj = {
id: 1,
name: 'andy',
msg: {
age: 18
}
};
var o = {};
Object.assign(o, obj);
console.log(o);
function deepCopy(newobj, oldobj) {
for (var k in oldobj) {
var item = oldobj[k];
if (item instanceof Array) {
newobj[k] = [];
deepCopy(newobj[k], item)
} else if (item instanceof Object) {
newobj[k] = {};
deepCopy(newobj[k], item)
} else {
newobj[k] = item;
}
}
}
deepCopy(o, obj);