I. Clear () →array
function Clear () { this. length = 0; return This ; }
Returns an empty array that clears item.
Example:
var fruits = [' Apple ', ' Orange ', ' Bananas ', ' Peach '];fruits.clear (); // , [] Fruits // , []
Two. Clone () →array
function Clone () { return slice.call (this, 0); }
Returns a copy of the array, keeping the original array intact.
Iii. Compact () →array
function Compact () { returnthis. Select (function(value) { return null; }); }
Returns a copy that does not contain null and undefined
Example:
var null, ' C '); var copy = orig.compact (); // orig, [Undefined, ' A ', Undefined, ' B ', null, ' C ']; // copy---[' A ', ' B ', ' C '];
Iv. every ([iterator = prototype.k[, context]) →boolean
iterator ( Function ) -An optional function to evaluate each element in the enumeration; The function should return a value to test. If not provided, the element itself is tested.
context ( Object ) - An optional object that is used in a this call to an iterator .
Determines whether all elements are true (boolean-equivalent true), calculated either directly or through the provided iterator. (I can't quite understand this one.)
functionevery (iterator) {if( This==NULL)Throw NewTypeError (); Iterator= Iterator | |PROTOTYPE.K; varcontext = Arguments[1]; varObject = Object ( This); for(vari = 0, length = object.length >>> 0; i < length; i++) { if(IinchObject &&!Iterator.call (context, object[i], I, object)) { return false; } } return true; } if(arrayproto.every) {every=wrapnative (Array.prototype.every); }
V.. filter (iterator[, context]) →array
iterator ( Function ) -An iterator function that is used to test the element.
context ( Object ) - this An optional object that is used in a call to an iterator .
returns a new array containing all the items in this array, where iterator returns a truth value.
functionFilter (iterator) {if( This==NULL|| !object.isfunction (iterator))Throw NewTypeError (); varObject = Object ( This); varresults = [], context = Arguments[1], value; for(vari = 0, length = object.length >>> 0; i < length; i++) { if(Iinchobject) {Value=Object[i]; if(Iterator.call (context, value, I, object)) {Results.push (value); } } } returnresults; } if(arrayproto.filter) {//' Array#filter ' requires an iterator by nature, so we don ' t need to //Wrap it.Filter =Array.prototype.filter; }
Vi.. First ()
function First () { returnthis [0]; }
returns the first item of an array (for example, array[0] ).
Vii.. Flatten () →array
function Flatten () { returnthisfunction(array, value) { if (Object.isarray (value)) return Array.concat (Value.flatten ()); Array.push (value); return array; }); }
Personal understanding: Merges all arrays within the specified array.
Official Understanding:
returns a flat (one-dimensional) copy of the array, keeping the original array intact. nested arrays are injected recursively inline.
Commonly used in processing the results of recursive collection algorithms.
Example:
var a = [' Frank ', [' Bob ', ' Lisa '],[' Jill ', [' Tom ', ' Sally ']] ; var b = a.flatten (); // a // b -> [' Frank ', ' Bob ', ' Lisa ', ' Jill ', ' Tom ', ' Sally ']
Viii.. IndexOf (item[, offser = 0]) →number
functionindexOf (item, i) {if( This==NULL)Throw NewTypeError (); varArray = Object ( This), length = Array.Length >>> 0; if(length = = 0)return-1; //The rules for the ' fromIndex ' argument is tricky. Let ' s follow the //spec Line-by-line.i =Number (i); if(IsNaN (i)) {i= 0; } Else if(I!== 0 &&isfinite (i)) { //equivalent to ES5 ' s ' tointeger ' operation.i = (i > 0? 1:-1) *Math.floor (Math.Abs (i)); } //If The search index is greater than the length of the array, //return-1. if(i > Length)return-1; //If The search index is negative and take its absolute value, subtract it //From the length, and make the new search index. If it ' s still //negative, make it 0. vark = i >= 0? I:math.max (Length-math.abs (i), 0); for(; k < length; k++) if(kinchArray && array[k] = = = Item)returnK; return-1; }
Official Understanding:
item( ? ) -values that might or may not exist in the array.
offset( Number ) -the number of initial items to skip before starting the search.
Returns the first occurrence of an index in the item array, or 1 if item does not exist in the array. Array#indexof uses absolute equals (= = =) to compare items.
Personal understanding: Returns the index of the position where item first appears in the array, because it distinguishes between the use of absolute equals so case-sensitive, type.
Example:
[3, 5, 6, 1, 20].indexof (1)// -- 3 [3, 5, 6, 1, 20].indexof (+)// -> ; -1 (not found) [' 1 ', ' 2 ', ' 3 '].indexof (1); // -1 (not found, 1!== ' 1 ')
Add an example that is not an array:
var str= "Hello world!" document.write (Str.indexof ("Hello") + "<br/>") document.write (Str.indexof("World") + "< br/> ") document.write (Str.indexof (" World "))// ->0// ->- 1// ->6
Ix.. Inspect () →string
function Inspect () { returnthis. Map (object.inspect). Join (', ') + '] '; }
Returns a debug-oriented string representation of an array.
Example:
[' Apples ', {good: ' Yes ', bad: ' No ', 3,].inspect () // "[' Apples ', [Object Object], 3,"] "
X.. Intersect (Array) →array
function intersect (array) { returnthis. Uniq (). FindAll (function(item) { return array.indexof (item)!==-1; });
Returns an array of each of the same items that are contained in two of a given array.
Array aspect JS bottom code learning record