-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
153 lines (118 loc) · 5.08 KB
/
array.js
File metadata and controls
153 lines (118 loc) · 5.08 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Array
// Array is dynamically sized
var colors = new Array();
var colors = new Array(20);
var colors = new Array("red", "blue", true);
// omitting new operator
var colors = Array(3);
var colors = Array("red");
// use array literal notation to create an array
var colors = [];
var colors = [1, 2, 3];
// append / insert new element
var colors = [0, 1, 2, 3];
colors[colors.length] = 4; // [0, 1, 2, 3, 4]
colors[7] = 7; // [0, 1, 2, 3, 4, undefinded, undefined, 7]
// length property of array can be written
var colors = [0, 1, 2];
colors.length = 10; // [0, 1, 2, undefined, ..., undefined]
colors.length = 1; // [0], colors[1] undefined
// detecting an array
var myArray = [];
if (Array.isArray(myArray)) {
// any statement here
}
if (myArray instanceof Array) {
// not recommended, because instanceof assumes a single global execution context
// if using multiple frames in a web page, there could be different version of Array constructor
}
colors.toString();
colots.toLocaleString();
colors.valueOf();
colors.join(';');
/* Stack methods for Array */
var colors = new Array();
colors.push("red", "green");
colors.push("black"); // ["red". "green", "black"]
var topItem = colors.pop(); // "black"
/* Queue Methods for Array */
var colors = new Array();
colors.push("red", "green");
var firstItem = colors.shift(); // "red"
colors.length; // 1
colors.unshift("newFirstItem"); // ["green", "newFirstItem"]
/* Reordering Methods */
var colors = [0, 1, 10, 15, 5];
colors.reverse();
colors.sort(); // default sort calls toString() of each item, and compare these strings. [0, 1, 10, 15, 5]
// I think default method is not well designed. Use toString() rather than valueOf() can avoid unmatch type comparing,
// but it makes hard for developers being aware of the sorting result.
// It can lead to unexpected result totally.
// asending order for numeric types, and objects whose valueOf() method returns numeric types
function asendingCompare(item1, item2) {
return item1 - item2;
}
colors.sort(asendingCompare); // [0, 1, 5, 10, 15]
/* concat() method */
var colors = ["red", "green", "blue"];
// concat method creates a new array, copying items in original array, appending all new flattened(1 layer) items in the end;
var colors2 = colors.concat("yellow", ["black", "brown"]); // ["red", "green", "blue", "yellow", "black", "brown"]
colors.concat("yellow", ["black", ["black", "red"]]); // ["red", "green", "blue", "yellow", "black", Array(2)]
/* slice() method */
var colors = ["red", "green", "blue", "yellow", "purple"];
var colors2 = colors.slice(1); // ["green", "blue", "yellow", "purple"]
var colors3 = colors.slice(1,4); // ["green", "blue", "yellow"]
/* splice() method, the most powerful method in array */
// splice mothod always return an array
var colors = ["red", "green", "blue"];
var removed = colors.splice(0, 2); // remove 2 items from index 0, and return an array with removed items
var colors = ["red", "green", "blue"];
var removed = colors.splice(1, 0, "yellow", "orange"); // insert two items at position 1
// remove 0 item at index 1, and insert the following items: so just insert
// removed is an array with removed items. In this case, an empty array
var colors = ["red", "green", "blue"];
var removed = colors.splice(1, 1, "red", "purple"); // insert two values, remove one
// remove 1 item at index 1, and insert the following items: so replace
/* indexOf(), lastIndexOf() */
// return -1 if not found
// use strictly equal method, ===
var alice = { name: 'Alice' };
var personArray = [ {name: 'Alice'} ];
personArray.indexOf(person); // -1, not found
personArray.indexOf({name: 'Alice'}); // -1, not found
var personArray = [alice];
personArray.indexOf(alice); // 0, found
/* every(), some(), filter(), map(), forEach() */
// all these 5 methods take a function as parameter, and this function takes (item, index, array) as parameters
/* every(), some(), both return true/false */
var numbers = [1,2,3,4,5,4,3,2,1];
// return false, not every item in the array meets with the condition.
var everyResult = numbers.every(function(item, index, array){
return (item > 2);
});
// return true, at least one item in the array meets with the condition.
var someResult = numbers.some(function(item, index, array){
return (item > 2);
});
/* filter(), return a new array by selecting items meeting with condition */
var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
}); // [3,4,5,4,3]
/* map(), operate on each item and return a new array */
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map(function(item, index, array){
return item * 2;
}); // [2,4,6,8,10,8,6,4,2]
/* forEach(), has no return value, similar with for loop */
var numbers = [1,2,3,4,5,4,3,2,1];
numbers.forEach(function(item, index, array){
// do something here
});
/* reduce(), reduceRight(), iterate all items and build up a value that is ultimately returned */
// reduce() iterate from left to right
// reduceRight(), iterate from right to left
var items = [1, 2, 3, 4, 5];
var sum = items.reduce(function(prev, cur, index, array){
return prev + cur;
});