This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js
66 lines (56 loc) · 2.48 KB
/
js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
> 20190410 一句话搞定5星评分
"★★★★★☆☆☆☆☆".substring(5 - rating, 10 - rating);
> 数组去重 https://dmitripavlutin.com/javascript-array-from-applications/
function unique(array) {
return Array.from(new Set(array));
}
unique([1, 1, 2, 3, 3]); // => [1, 2, 3]
> 合并对象
let form = {name: 'liming', sex: '男'};
let obj = {class: '一班', age: 15};
Object.assign(form, obj);
> 对象转 json
JSON.stringify(obj);
> 判断对象是否为空
1. JSON.stringify(obj) === '{}'
2. Object.keys(obj).length === 0
> 判断对象是不是数组
var arr = [1,2,3,1];
alert(arr instanceof Array); // true
alert(arr.constructor === Array); // true
> 检测数组 site 是否包含 runoob
let site = ['runoob', 'google', 'taobao'];
site.includes('runoob'); // true
> console.log / color
console.log("%c U %c Want something shining ",'background: linear-gradient(to right, #09f0fb, #047edf);color:#fff;border-radius:2px 0 0 2px', 'background:#673ab7;border-radius:0 2px 2px 0; color:#fff;');
- 'background: linear-gradient(to right, #09f0fb, #047edf);color:#fff;border-radius:2px 0 0 2px', 'background: linear-gradient(to right, #047edf, #b66dff);border-radius:0 2px 2px 0; color:#fff;' - #673ab7
> 交互式标题
var OriginTitile = document.title, st;
document.addEventListener("visibilitychange", function () {document.hidden ? (document.title = "看不见我🙈~看不见我🙈~", clearTimeout(st)) :
(document.title = "(๑•̀ㅂ•́) ✧被发现了~", st = setTimeout(function () {document.title = OriginTitile}, 3e3))})
> 正则
str.replace(/[^0-9\;]/ig,"");
str.replace(/<[^>]+>/g,""); // 取出 html
/(?<=i\/).*?(?=\/)/ // 两个特殊符号之间的内容
var str = 'trem-1234';
var s=/\w+\-(\d+)/.exec(str);
# 先行断言
/\d+(?=%)/.exec('100% of US presidents have been male') // ["100"]
/\d+(?!%)/.exec('that’s all 44 of them') // ["44"]
# 后行断言
/(?<=\$)\d+/.exec('Benjamin Franklin is on the $100 bill') // ["100"]
/(?<!\$)\d+/.exec('it’s is worth about €90') // ["90"]
# 具名组
1) let {groups: {one, two}} = /^(?<one>.*):(?<two>.*)$/u.exec('foo:bar');
one // foo
two // bar
2) let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
'2015-01-02'.replace(re, '$<day>/$<month>/$<year>')
// '02/01/2015'
> 获取字符串长度
function codePointLength(text) {
var result = text.match(/[\s\S]/gu);
return result ? result.length : 0;
}
> 判断一个字符在另一个字符串中出现的次数
'1.2.3....'.split('.').length - 1