/**Array对象***********************************************************/
console.log("**Array对象***********************************************************");
var myRe = /d(b+)(d)/i;
console.log(myRe.exec("cdbBdbsbz"));
//1,concat ( values ) : Array
/*PARAMETERS
values : Object...
Arrays and/or values to concatenate to the resulting array.
RETURNS :Array
New array.*/
console.log([1, 2, 3].concat(4, [5, 6], [7, 8], 9));//[1,2,3,4,5,6,7,8,9]
//2,every ( callback, [thisObject] ) : Boolean
/*PARAMETERS
callback : Function
Function to test for each element.
value : Mixed The element value.
index : Number The element index.
array : Array The array being traversed.
return : Boolean Should return true when element passes the test.
thisObject : Object (optional) Object to use as this when executing callback.
RETURNS :Boolean True when all elements pass the test.*/
var isMoreThan1 = [1, 2, 3].every(function (elem, index, arr) {
return elem > this.limit;
}, {limit: 1});
console.log(isMoreThan1);//false
//3,filter ( callback, [thisObject] ) : Array
/*Creates a new array with all elements that pass the test implemented by the provided function.
* PARAMETERS
callback : Function Function to test for each element.
value : Mixed The element value.
index : Number The element index.
array : Array The array being traversed.
return : Boolean Should return true when element passes the test.
thisObject : Object (optional) Object to use as this when executing callback.
RETURNS :Array Array of elements that passed the test.*/
console.log([1, 2, 3].filter(function (elem, index, arr) {//[2,3]
return elem > this.limit;
}, {limit: 1}));
//4,forEach ( callback, [thisArg] )
/*Executes a provided function once per array element.
PARAMETERS
callback : Function Function to execute for each element.
value : Mixed The element value.
index : Number The element index.
array : Array The array being traversed.
thisArg : Object (optional) Object to use as this when executing callback.*/
[1, 2, 3].forEach(function (elem, index, arr) {
console.log(this.limit + "[" + index + "]=" + elem);
}, {limit: 3});
//5,indexOf ( searchElement, [fromIndex] ) : Number
/* indexOf compares searchElement to elements of the Array using strict equality
(the same method used by the ===, or triple-equals, operator).
Returns the first index at which a given element can be found in the array,
or -1 if it is not present*/
console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 1].indexOf(5, 3));//4,注意fromIndex不会影响结果
console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, {age: 1}].indexOf({age: 1}));//-1
//6,join ( separator ) : String
/*PARAMETERS
separator : String Specifies a string to separate each element of the array.
The separator is converted to a string if necessary.
If omitted, the array elements are separated with a comma.//默认是逗号
RETURNS :String
A string of the array elements.*/
console.log([1, 2, 3].join("~"));//1~2~3
//7,lastIndexOf ( searchElement, [fromIndex] ) : Number
/* PARAMETERS
searchElement : Mixed Element to locate in the array.
fromIndex : Number (optional) The index at which to start searching backwards.注意是向后的index
Defaults to the array's length, i.e. the whole array will be searched.
If the index is greater than or equal to the length of the array,
the whole array will be searched.
If negative, it is taken as the offset from the end of the array.
Note that even when the index is negative, the array is still searched from back to front.
If the calculated index is less than 0, -1 is returned, i.e.
the array will not be searched.
如果fromIndex小于0,那么可以理解fromIndex=array's length+fromIndex
个人认为fromIndex改成toIndex更合适~~
RETURNS :Number The index of element found or -1.*/
console.log([1, 2, 5, 9, 2, 3, 2].lastIndexOf(2));//6
console.log([1, 2, 5, 9, 2, 3, 2].lastIndexOf(2, -1));//6
console.log([1, 2, 5, 9, 2, 3, 2].lastIndexOf(2, 0));//-1
console.log([1, 2, 5, 9, 2, 3, 2].lastIndexOf(2, 1));//1
console.log([1, 2, 5, 9, 2, 3, 2].lastIndexOf(2, -2));//4
//8,map ( callback, [thisObject] ) : Array
/*1,不会改变原始的数组
* 2,返回值不会改变数组的长度
* 可以理解为对数组的组装,filter是过滤*/
console.log(["foot", "goose", "moose", "kangaroo"].map(function (elem, index, arr) {
var result = elem.replace(/o/g, 'e');
if (elem === 'kangaroo') {
result += 'se' + this.limit;
}
return result;
}, {limit: 3}));//[ 'feet', 'geese', 'meese', 'kangareese3' ]
console.log([4, 9, 16].map(Math.sqrt));//[ 2, 3, 4 ]
//9,pop Object
/*The pop method removes the last element from an array and returns that value to the caller.
* RETURNS :Object The last element in the array*/
console.log([1, 2, 3].pop());//3
//10,push ( elements ) : Number
/*PARAMETERS
elements : Object... The elements to add to the end of the array.
RETURNS :Number The new length property of the object upon which the method was called.
注意与concat的区别
1,push参数只能是元素(数组的话,当一个元素对待),concat参数可以是元素也可以是数组
2,push会改变原始的数组,concat不会改变
3,push返回改变后的数组长度,concat返回改变后的数组*/
console.log([1, 2, 3].push(4, 5, 6));
var p = [1, 2, 3];
var c = [1, 2, 3];
p.push(4, 5);
c.concat([4, 5]);
console.log(p);//[ 1, 2, 3, 4, 5 ]
console.log(c);//[ 1, 2, 3 ]
console.log(c.concat([4, 5]));//[ 1, 2, 3, 4, 5 ]
//11,reduce ( callback, [initialValue] ) : Mixed
/*目的是通过数组的元素,计算出某个值来.
PARAMETERS
callback : Function Function to execute on each value in the array.
previousValue : Mixed The value previously returned in the last invocation of the `callback`, or `initialValue`, if supplied.
currentValue : Mixed The current element being processed in the array.
index : Number The index of the current element being processed in the array.
array : Array The array reduce was called upon.
initialValue : Mixed (optional) Object to use as the first argument to the first call of the callback.
RETURNS :Mixed
The value returned by final invocation of the callback.*/
[1, 2, 3, 4, 5].reduce(function (prev, cur, index, arr) {
if (typeof prev == "object") {
console.log(prev.limit + "+" + cur + "+" + index);
return prev.limit + cur;
}
else {
console.log(prev + "+" + cur + "+" + index);
return prev + cur;
}
}, {limit: 4});
/*4+1+0
5+2+1
7+3+2
10+4+3
14+5+4
*/
//12,reduceRight ( callback, [initialValue] ) : Mixed
/*和reduce只是顺序不同,
reduce是从左向右,index=0..length-1;
* reduceRight是从右向左,index=length-1..0*/
//13,reverse Array 注意,这个改变原始数组
var r = [1, 2, 3];
r.reverse();
console.log(r);//[ 3, 2, 1 ]
//14,shift Object 删除第一个元素,并且返回第一个元素
var myFish = ["angel", "clown", "mandarin", "surgeon"];
console.log("myFish before: " + myFish);
var shifted = myFish.shift();
console.log("myFish after: " + myFish);
console.log("Removed this element: " + shifted);
//14,slice ( begin, end ) : Array 不包括end
/*Extracts a section of an array and returns a new array.
* slice does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array.
* Elements of the original array are copied into the new array as follows:
For object references (and not the actual object), slice copies object references into the new array.
Both the original and new array refer to the same object.
If a referenced object changes, the changes are visible to both the new and original arrays.
For strings and numbers (not String and Number objects), slice copies strings and numbers into the new array.
Changes to the string or number in one array does not affect the other array.
If a new element is added to either array, the other array is not affected.
PARAMETERS
begin : Number Zero-based index at which to begin extraction.
As a negative index, start indicates an offset from the end of the sequence.
slice(-2) extracts the second-to-last element and the last element in the sequence
end : Number Zero-based index at which to end extraction. slice extracts up to but not including end.
slice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3).
As a negative index, end indicates an offset from the end of the sequence.
slice(2,-1) extracts the third element through the second-to-last element in the sequence.
If end is omitted, slice extracts to the end of the sequence.
RETURNS :Array
Array from the new start position up to (but not including) the specified end position.
对象的话,直接复制引用,结果和原始值指向同一地址;字符串或数字的话,直接拷贝值,结果和原始值不受影响.
这两个参数还能为负数...*/
var sliceOldArr = [{limit: 5}, 2, 3];
var sliceArr = sliceOldArr.slice(0, -1);
sliceArr[0].limit = 6;
console.log(sliceOldArr);//[ { limit: 6 }, 2, 3 ]
//15,some ( callback, [thisObject] ) : Boolean
/*与every区别
* every所有的元素符合条件后,为true;some一个条件符合后,为true*/
//16,sort ( compareFunction ) : Array
var s1 = [2,3,4,1,6,3];
s1.sort();//可以忽略比较函数
console.log(s1);//[ 1, 2, 3, 3, 4, 6 ]
s1.sort(function (a,b) {
return b - a;
});
console.log(s1);//[ 6, 4, 3, 3, 2, 1 ]
//16,splice ( index, howMany, elements ) : Array 拼接数组,可以用来删除数组中间的元素.
/*PARAMETERS
index : Number
Index at which to start changing the array. If negative, will begin that many elements from the end.
howMany : Number
An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. If no howMany parameter is specified all elements after index are removed.
elements : Object...
The elements to add to the array. If you don't specify any elements, splice simply removes elements from the array.
RETURNS :Array
An array containing the removed elements. If only one element is removed, an array of one element is returned..*/
var s2 = [1,2,3,4,5];
s2.splice(2,1);//返回 [3]
console.log(s2);//[ 1, 2, 4, 5 ]
s2.splice(2,0,3);
console.log(s2);//[ 1, 2, 3, 4, 5 ]
//17,unshift ( elements ) : Number
/*push是向数组后面插入,unshift是向数组前面插入,都是返回插入后数组长度*/
console.log([2,3,4].unshift(1));//4
//18, isArray ( obj ) : Boolean 静态方法
console.log(Array.isArray([1,2,3]));//true
/*[].pop(); //移除最后一个元素
[].push(); //元素增加到最后一个
[].shift(); //移除第一个元素
[].unshift();//元素增加到第一个*/
http://docs.sencha.com/extjs/6.2.1/modern/Array.html#method-shift