javascript-常用方法

0x01 数组–去重复

1
2
3
4
5
6
7
8
9
10
function unique(array) {
var res = [];
for (var i = 0, len = array.length; i < len; i++) {
var current = array[i];
if (res.indexOf(current) === -1) {
res.push(current)
}
}
return res;
}

0x02 对象–统计参数和出现的重复次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function uniqueCount(arr) {
var a = [], b = [], prev;

arr.sort();
for ( var i = 0; i < arr.length; i++ ) {
if ( arr[i] !== prev ) {
a.push(arr[i]);
b.push(1);
} else {
b[b.length-1]++;
}
prev = arr[i];
}

return [a, b];
}

0x03 对象–对应的键和值进行排序

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
var trend_title = ["01","02", 
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"00" ,
];

var trendResult = {
"01": 0,
"02": 0,
"03": 0,
"04": 0,
"05": 0,
"06": 0,
"07": 0,
"08": 0,
"09": 0,
"10": 0,
"11": 0,
"12": 0,
"13": 0,
"14": 0,
"15": 0,
"16": 0,
"17": 0,
"18": 0,
"19": 0,
"20": 0,
"21": 0,
"22": 0,
"23": 0,
"00": 0
}

var trend_data = [];
for(var i in trend_title){
trend_data.push(trendResult[trend_title[i]]) ;
}

0x04 vue-table索引替换

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
result = []  为table数据

// 索引上移
up(index) {
if (index === 0) {
return;
}
this.swapItems(this.results, index, index - 1);
},
// 索引置顶
upIndex(index) {
if (index === 0) {
return;
}
this.swapItems(this.results, index, 0, "up");
},
// 索引下移
down(index) {
if (index === this.results.length - 1) {
return;
}
this.swapItems(this.results, index, index + 1);
},
// 索引置底
downIndex(index) {
if (index === this.results.length - 1) {
return;
}
this.swapItems(this.results, index, 0, 'down');
},
// 交换索引
swapItems(arr, index1, index2, direction) {
if (direction == 'up') { //置顶
arr.unshift(arr[index1]);
arr.splice(index1 + 1, 1);
return arr;
}
if (direction == 'down') { //置底
arr.push(arr[index1]);
arr.splice(index1, 1);
return arr;
}
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
},

0x05 时间分割

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function DateFormt(date,fmt) {
if(date instanceof Date){}else{
date = new Date()
}
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

function X_list( etime,dtime,T){
T = T || "10m"
// 10m 1h 1d
var relist = []
if(T=="10m"){
while(true){
if(etime>dtime){
break
}
var str_date = DateFormt(etime,"yyyy-MM-dd")
//relist = []
for(var i=0;i<24;i++){
for(var ii=0;ii<=5;ii++){
var hh = ""
var mm = ""
hh = (i.toString().length == 1) ? ("0"+i.toString()) : (i.toString())
mm = ii.toString()+"0"
relist.push(str_date+' '+hh+mm)
}
}
etime.setDate(etime.getDate()+1)
}
}
else if(T=="1h"){
var tmp_etime = new Date(etime)
var tmp_dtime = new Date(dtime)
tmp_dtime.setDate(tmp_dtime.getDate()+1)
var all_list = X_list(tmp_etime,tmp_dtime)
var j = 1
while(true){
if(etime>dtime){
break
}
var str_date = DateFormt(etime,"yyyy-MM-dd")
for(var i=0;i<24;i++){
var hh = ""
hh = (i.toString().length == 1) ? ("0"+i.toString()) : (i.toString())
var tmp10m = []
for(var ii = 0;ii<6;ii++){
tmp10m.push(all_list[j])
j++
}
relist[str_date+' '+hh] = tmp10m
}
etime.setDate(etime.getDate()+1)
}
}
else{
var tmp_etime = new Date(etime)
var tmp_dtime = new Date(dtime)
tmp_dtime.setDate(tmp_dtime.getDate()+1)
var all_list = X_list(tmp_etime,tmp_dtime)
var j = 1
while(true){
if(etime>dtime){
break
}
var str_date = DateFormt(etime,"yyyy-MM-dd")
var tmp10m = []
for(var ii=0;ii<144;ii++){
tmp10m.push(all_list[j])
j++
}
relist[str_date] = tmp10m
etime.setDate(etime.getDate()+1)
}
}
return relist
}


var tmp_etime = new Date()
var tmp_dtime = new Date(tmp_etime)
// tmp_dtime.setDate(tmp_dtime.getDate()-7)
// console.log( tmp_etime, tmp_dtime );
console.log( X_list(tmp_dtime ,tmp_etime, '1h') );

获取object的键

1
2
var test = {"f":"2323", "d": "test"};
console.log( Object.keys(test) );
坚持原创技术分享,您的支持将鼓励我继续创作!