String
method |
describe |
charAt() |
Returns the character at the specified position. |
charCodeAt() |
Returns the Unicode encoding of the character at the specified position. |
concat() |
connection string. |
indexOf() |
Retrieve string. |
match() |
Find a match for one or more regular expressions. |
replace() |
Replace substrings that match the regular expression. |
search() |
Retrieve values that match the regular expression. |
slice() |
Extract a fragment of a string and return the extracted part in a new string. |
split() |
Split a string into an array of strings. |
toLocaleLowerCase() |
Convert the string to lowercase. |
toLocaleUpperCase() |
Convert a string to uppercase. |
toLowerCase() |
Convert the string to lowercase. |
toUpperCase() |
Convert a string to uppercase. |
substr() |
Extracts the specified number of characters in the string from the starting index number. |
substring() |
Extracts the characters between two specified index numbers in a string. |
array
method |
describe |
slice[start,end) |
Returns a new array consisting of items from the specified start index to the end index in the original array (does not affect the original array) |
. |
1 parameter: n. ie: all from n to the end |
. |
2 parameters: [start,end] |
splice(): |
|
. |
delete: 2 parameters, starting position, number of deleted items |
. |
Insert: 3 parameters, starting position, number of deleted items, inserted items |
. |
Replace: any argument, starting position, number of items to delete, insert any number of items |
pop() |
Removes the last element of the array, reduces the length of the array, and returns the removed value. (without parameter) |
push() |
Load the arguments to the end of the array and return the length of the new array. (parameters are not limited) |
shift() |
Delete the first element of the array, decrease the length of the array by 1, and return the deleted value. (without parameter) |
unshift() |
Adds one or more elements to the beginning of the array and returns the new length. (parameters are not limited) |
sort() |
Sorts the array according to the specified parameters, and the returned value is the sorted array (no parameters/function) |
concat(3,4) |
Concatenate the two arrays together. The returned value is a copy (parameters are not limited) |
join() |
Group the elements of the array into a string, with the separator as the separator, if omitted, the default comma is used as the separator |
indexOf() |
Look backwards from the beginning of the array, accepts two parameters, the item to look for (optional) and the index of the starting position to look for |
lastIndexOf() |
从数组末尾开始向前查找,接受两个参数,要查找的项(可选)和查找起点位置的索引 |
every() |
对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true。 |
filter() |
对数组中的每一项运行给定函数,返回该函数会返回true的项组成数组。 |
forEach() |
对数组的每一项运行给定函数,这个方法没有返回值。 |
map() |
对数组的每一项运行给定函数,返回每次函数调用的结果组成的数组。 |
some() |
对数组的每一项运行给定参数,如果该函数对任一项返回true,则返回true。以上方法都不会修改数组中的包含的值。 |
reduce()和reduceRight() |
缩小数组的方法,这两个方法都会迭代数组的所有项,然后构建一个最终返回的值。 |
数组测试用例:
var ages = [32, 33, 16, 40];
function checkAdult(age) {
return age >= 18;
}
// every
print(ages.every(checkAdult)) // false
print(ages.some(checkAdult)) // true
//filter
print(ages.filter(checkAdult)) // [32, 33, 40]
// forEach
var arr = [1,2,3,4];
arr.forEach(function(ele) {
console.log(ele)
});
// 1 2 3 4
var arr = [1,2,3,4];
var add = function (ele) {
ele++;
return ele;
}
print(arr.map(add)); // 返回数组[2, 3, 4, 5]
//reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值。
//例子:将数组所有项相加
var total = [0, 1, 2, 3].reduce(function(a, b) {
return a + b;
}, 0);
print(total) // 6 = 0 + 1 + 2 + 3
// reduceRight() 方法接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值。
//例子
let flattened = [
[0, 1],
[2, 3],
[4, 5]
].reduceRight((a, b) => {
return a.concat(b);
}, []);
print(flattened) [4, 5, 2, 3, 0, 1]
Math
方法 |
描述 |
ceil(x) |
尽可能取最大。 |
floor(x) |
尽可能取最小。 |
round(x) |
把数四舍五入为最接近的整数。 |
max(x,y) |
返回 x 和 y 中的最高值。 |
min(x,y) |
返回 x 和 y 中的最低值。 |
pow(x,y) |
返回 x 的 y 次幂。 |
random() |
返回 0 ~ 1 之间的随机数。 |
sqrt(x) |
返回数的平方根。 |
正则
方法 |
描述 |
compile |
编译正则表达式。 |
exec |
检索字符串中指定的值。返回找到的值,并确定其位置。 |
test |
检索字符串中指定的值。返回 true 或 false。 |
search |
检索与正则表达式相匹配的值。 |
match |
找到一个或多个正则表达式的匹配。 |
replace |
替换与正则表达式匹配的子串。 |
split |
把字符串分割为字符串数组。 |