-
Notifications
You must be signed in to change notification settings - Fork 1
/
array-ops.js
95 lines (73 loc) · 1.59 KB
/
array-ops.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
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
// Source: [email protected]:tooto1985/js-array-operations.git
//Example
var ary = ["A", "B", "C", "D", "E"];
//length
console.log(ary.length); //5
//push
console.log(ary.push("F")); //6
//pop
console.log(ary.pop()); //"F"
//unshift
console.log(ary.unshift("F")); //6
//shift
console.log(ary.shift()); //"F"
//splice(insert)
console.log(ary.splice(3, 0, "F")); //[]
//splice(remove)
console.log(ary.splice(3, 1)); //["F"]
//indexOf
console.log(ary.indexOf("D")); //3
//lastIndexOf
console.log(ary.lastIndexOf("D")); //3
//join
console.log(ary.join(":")); //"A:B:C:D:E"
//reverse
console.log(ary.reverse()); //["E", "D", "C", "B", "A"]
//sort
console.log(ary.sort()); //["A", "B", "C", "D", "E"]
//some
console.log(
ary.some(function (item) {
return item === "C";
}),
); //true
//every
console.log(
ary.every(function (item) {
return item.length === 1;
}),
); //true
//forEach
console.log(
ary.forEach(function (item) {
return item;
}),
); //undefined
//reduce
console.log(
ary.reduce(function (prevItem, item) {
return prevItem + item;
}),
); //"ABCDE"
//reduceRight
console.log(
ary.reduceRight(function (prevItem, item) {
return prevItem + item;
}),
); //"EDCBA"
//map
console.log(
ary.map(function (item) {
return item + item;
}),
); //["AA", "BB", "CC", "DD", "EE"]
//filter
console.log(
ary.filter(function (item) {
return item > "C";
}),
); //["D", "E"]
//slice
console.log(ary.slice(2, 4)); //["C", "D"]
//concat
console.log(ary.concat(["G", "H"])); //["A", "B", "C", "D", "E", "G", "H"]