19.4 Array API Supplement
1. Array.reverse (): Flips the data order in the array, returning a new array after flipping.
API Description:
/**
@return {array.<t>}//returns an array.
*/
Array. prototype. reverse = function () {};
2. Array.filter (function (Element,index,salarys) {}): Iterates through all the elements in the array, filters the data within the array as required in the function, returns true in the function to leave, returns the Falser filter out, Returns a new array that is filtered out. Such as:
var Newsalarys = salarys.filter (function (Element,index,salarys) {
Console.log (Element); Each item element in the array
Console.log (index);//index of each element in the array
Console.log (Salarys);//Current array
});
API Description:
/**
@param {function (t=, number=, array.<t>=)} callback//need to set a callback function.
@param {*} [Thisarg]//Optional parameters, also can not be selected.
@return {array.<t>}//Returns a function.
*/
Array.prototype.filter = function (Callback,thisarg) {};
3. Array.foreach (function (Element,index,salarys) {}): This API only does the function of iterating through each item of an array, and can output each item in the array and the index of each item through the code within the function. However, there is no return value for this API, or the return value is undefined.
API Description:
/**
@param {function (t=, number=, array.<t>=)} callback//need to set a callback function.
@param {*} [Thisarg]//Optional parameters, also can not be selected.
@return {void}//returns an invalid value.
*/
Array.prototype.forEach = function (Callback,thisarg) {};
4. Array.indexof (Searchelement,fromindex); Returns the index position of an element in the array, if there is only one parameter searchelement, the default FromIndex to start with the index of 0. If there is a second argument fromindex, it looks for the same element as searchelement from the index of the parameter and returns the index of the element. Returns 1 if no element is the same as searchelement.
API Description:
/**
@param {T} searchelement//The element that needs to be selected.
@param {Number} [FromIndex]//The initial index position at the time of the lookup.
@return {Number}//returns the index position of the found element.
*/
Array.prototype.indexOf = function (Searchelement,fromindex) {};
5. Array.slice (start,end): intercepts the elements in the array to form a new array and returns without changing the original array.
1 [start,end] The first parameter is the index position at which to start the Intercept, the second is the index position at which to terminate the interception, but the value corresponding to the second index is not taken.
2 when there is only one parameter, this parameter is the index position at which to start the Intercept, and the last element is intercepted from the index position.
3 when the second value is less than the first value, the intercepted data is empty and an empty array is returned.
4 when there is only one parameter, and this parameter is negative, it is equivalent to starting to intercept the index to array.length+ the value of this parameter, and intercept all subsequent elements.
5 when the second value is negative, the index that terminates the intercept is the value of the array.length+ parameter. However, the value of the second index is not taken.
API Description:
/**
@param {Number} [Start]//The index position at which to start the intercept.
@param {Number} [end]//The index position at which to terminate the intercept.
@return {array.<t>}//returns an array.}
*/
Array. prototype. Slice = function (start,end) {};
6. Array.splice (Start,deletecount, items):
1 Delete the elements in the array: only start,deletecount two parameters is the deletion function, the first parameter is the index position to start the Intercept, the second parameter is the number of intercept elements, returns a new array consisting of the intercepted elements, the corresponding elements of the original array will be deleted.
2 Replace the elements in the array: Start,deletecount,items three parameters are equal to the function of replacing the elements in the original array with the new elements. The first parameter is the index position at which to start the Intercept, the second parameter is the number of intercepted elements, and returns a new array consisting of the intercepted elements, for the original array to delete the intercepted elements, and then the new elements in items to fill the location of the interception, the number of elements intercepted and the number of elements added can be inconsistent.
API Description:
/**
@param {Number} [start]//the index position at which to start intercepting.
@param {Number} [deletecount]//need to intercept the number of elements.
@param {... T} [items]//replace the element.
@return {array.<t>}//returns an array.}
*/
Array. prototype. Splice = function (Start,deletecount,items) {};
20. How to Empty an array
20.1 Array.splice () method: Intercept from 0, intercept the number of elements of the entire array, that is, the arr.length.
20.2 How to change the length of the array: make the length of the array 0 to empty the array.
20.3 directly re-assigns an empty array, and this method is used in general programming.
21. Properties and methods for string strings
We can treat a string as an array of many characters, and it will have many properties and methods for the array:
21.1 String.Length Properties
You can view the character length of a string through the properties of String.Length, and you can also use a looping statement to traverse all the characters in a string.
21.2 String.Concat (strings) connect two strings
Strings 1 and 2 are concatenated by means of the String1.concat (string2) method.
API Description:
/**
@param {... string} [The strings]//parameter is a string.]
@return {string}//returns a new string.}
*/
String. prototype. concat = function (strings) {};
21.3 String.slice (Start,end)
The slice usage in the string is consistent with the usage in the array.
21.4 string.substring (Start,end)
It is basically consistent with the usage of slice.
The difference is in the special usage: When two parameters, and the second parameter is negative, then the second parameter is relative to 0, and then the element index is 0 to start interception, the first parameter is the Stop intercept index position, and intercept this value, the intercepted element is composed of a new array returned.
/**
@param {Number} The index position at which the start//starts to intercept.
@param {Number} [end]//terminates the Intercept to get the index position.]
@return {string}//returns a new string.}
*/
String. prototype. substring = function (start,end) {};
21.5 string.substr (Start,length)
12 parameters, the first parameter is the index position at which to start the Intercept, and the second parameter is the number of elements intercepted. and returns a new array of captured elements.
2 A parameter, the first parameter is the index position at which to start the Intercept, and the last element is intercepted from the index position. and returns a new array of captured elements.
31 parameters and the parameter is negative, it is equivalent to array.length+ the value of this parameter is the index position to begin to intercept, and then intercept all subsequent elements. and returns a new array of captured elements.
42 parameters and the second parameter is negative, it is equal to the number of intercept element is zero, so the intercept result is empty, return an empty array.
API Description:
/**
@param The index position at which {number} start//begins to intercept.
@param {Number} [length]//intercepts the number of characters.
@return {string}//returns a new string.}
*/
String. prototype. substr = function (start,length) {};
21.6 String.Split (Separator,limit)
Splits a character in a string into one or more elements in the array, that is, the string is converted to a group.
The separator delimiter should be the character that appears in the string or an empty string, or the whole string will be converted to an element.
API Description:
/**
@param {*} separator//delimiter type.
@param {Number} [Limit]
@return {array}//returns a new array.}
*/
String. prototype. split = function (Separator,limit) {};
21.7 String.charat (Gets the character of the specified index position in the string)
API Description:
/**
@param {Number} pos//the specified index location.
@return {string}//Returns the character for that position.}
*/
String. prototype. charAt = function (pos) {};
21.8 string.charcodeat (Gets the ASCII value of the character at the specified index position in the string)
API Description:
/**
@return {number}//Returns an ASCII code value.}
*/
String. prototype. charCodeAt = function (index) {};
21.9 String Conversion case
var str = "Hellowmmmaorld";
console. log (Str.touppercase ());//converts all characters in a string to uppercase.
console. log (Str.tolocaleuppercase ());//converts all characters in a string to uppercase.
console. log (Str.tolowercase ());//converts all characters in a string to lowercase.
console. log (Str.tolocalelowercase ());//converts all characters in a string to lowercase.
21.10 Remove the space before and after the string String.Trim ()
API Description:
/**
@return {string}//returns a new string.}
*/
String.prototype.trim = function () {};
21.11 String.Replace () replaces characters inside a string
This method has two parameters,
The first parameter is searchvalue to the element that needs to be replaced, and the second parameter replacevalue to the replaced element. However, this method can only replace the first element with the same searchvalue parameter.
All substitutions can be implemented in a regular fashion:
The first parameter replaces all elements in the string that are identical to the parameter searchvalue by the representation of the/a/g (G represents the global overall meaning).
API Description:
/**
@param {regexp|string} searchvalue//the character to be replaced.
@param {string|function} replacevalue//the replaced character.
@return {string}//returns a new string.}
*/
String. prototype. replace = function (Searchvalue,replacevalue) {};
JS Basic Learning 07