Methods of integration arrays and strings

js in arrays and strings is somewhat similar, not the essence, but in the traversal, similar to the time of acquisition. From the identification, it can be seen that an array is, the string; However, the use traversal, inadvertently will confuse the two, resulting in the wrong method. At the same time there are several methods of both the same, but need to pay attention to semantics, as well as some methods will not affect the original array. Here are some methods on arrays and strings of my finishing, does not guarantee integrity.

Array Methods

arr.push ()       to add one or more elements to the end of the array, and returns the new length. It will affect the original array

1 var a = [1,2];
2 a.push(3);
3 console.log(a); // [1,2,3]
4 console.log(a.push(8));  //4

arr.unshift () was added at the beginning of the array element, the element returns the value. It will affect the original array

1 var a = [1,2,3,"hello"];
2 console.log(a.unshift("world")); // world
3 console.log(a); // ["world",1,2,3,"hello"]

arr.pop () array for ejecting the last element, the return value of the element. It will affect the original array. (Equivalent to delete this element in the array)

1 var a = [1,2];
2 console.log(a.pop()); // 2
3 console.log(a); // [1]

arr.shift () will be the first element of the array from which to delete, and returns the value of the first element. It will affect the original array. (Deletion)

var a = [1,2];
console.log(a.shift()); // 1
console.log(a); // [2]

arr.concat () is used to connect two or more arrays and returns a new array. It will not affect the original array.

1 var arr = [1,2,3,4];
2 alert(arr.concat(1,2,3,[1,2,3],5).length); //11
3 arr.concat(1,2,4,[1,2,3],5);
4 alert(arr.length)    //4

arr.join () receives as a parameter separator, returns a string. It will not affect the original array. It accepts a parameter or parameters 0, if only the first pass a plurality of parameters are valid. If the parameter is not, by default, ''

1 [1,2,3,4].join('0').split('')    //["1", "0", "2", "0", "3", "0", "4"]
2 var arr = [1,2,3,4]; arr.join("&");
3 console.log(arr)    //[1,2,3,4]

 

arr.slice (n, m): for cutting and create a new array will not affect the original array.

  • One parameter: intercepting from a starting position to the end
  • 2 parameters: taken from the start position to the end position [a, b) does not include an end position itself 
  • End position may also be negative (actually calculated as: the length of the array + negative) is smaller than the end position of the starting position, returns an empty array []
. 1  var A = [. 1, 2,. 3 ];
 2 the console.log (a.slice (. 1));       // [2,. 3] 
. 3 the console.log (a.slice (. 1, 2));       // [ 2] 
. 4 the console.log (A);        // [. 1, 2,. 3] the original array is not changed 
. 5 the console.log (a.slice (. 1, -1));      // (+ -1. 3) = 2 > a.slice (. 1, 2)> [2] 
. 6 the console.log (a.slice (. 1, -100));      // []

arr.splice (n, m, data) for intercepting data, insert data, delete data. Operation of the data itself, it will affect the original array.

  • [Start position]: When only one parameter: delete all data from the rest of the starting position of an array and returns the deleted data.
  • [Start position, the number of cut] (Note, the meaning of this parameter and the second slice is not the same, but, that is the length): as a starting point to n, m in length, delete [n, n + m) of the element
  • [Start position, the number of truncation, insertion data]: (n, m, data): starting with n, m in length, delete [n, n + m] elements, and filled to delete data position
1 var array = [15, 5, 10, 2, 0];
2 console.log(array.splice(1, 3,1,2,1));  //5,10,2
3 console.log(array);     //15, 1, 2, 1, 0
4 console.log(array.splice(1, 3));  //5,10,2
5 console.log(array);     // [15,0]
6 console.log(array.splice(1));     // 0
7 console.log(array);     //15

arr.reverse () array elements inverted, it will affect the original array.

1 var a = [1,2,3,5];
2 console.log(a.reverse());  // [5,3,2,1]

arr.sort (fn) sorting method parameters: fn (a, b): comparison function, when no parameters ASCII alphabetic sort ascending

1 var a = [1, 2, 3, 5, 10, 25];
2 console.log(a.sort());     // [1, 10, 2, 25, 3, 5]

sort()The default method for each call to transformation sub-items  toString(), only after judgment. And this time is actually based on the size of the ASCII code to judge. Thus "15" < "5." First compare the first, and then compare the second. Want to sort according to our expectations, the need for some processing.

1 var array = [15, 5, 10, 2, 0];
2 array.sort(function(a,b){
3         return a-b;
4 })
5 console.log(array);  //[0, 2, 5, 10, 15]

The following is the ES5 new method (the new method will not affect the original array)

arr.indexOf () returns the position of the first element in the array is found, returns -1 if there

1     var array = [15, 5, 10, 2, 0];
2     console.log(array.indexOf(1));       //-1
3     console.log(array.indexOf(15));     //0

arr.forEach (): for each array running for a given function, this method does not return value (iterate to obtain all array elements) parameter function type, there is a default parameter passing (through the array contents in the corresponding array index, array itself)

1     var array = [15, 5, 10, 2, 0];
2     array.forEach(function (value, index, array) {
3         console.log(array[index]); 
4     });

arr.map (): to run on each item in the array given function returns an array consisting of the results of each call.

1     var array = [15, 5, 10, 2, 0];
2     var num = array.map(function (value, index, array) {
3         return value * 3;
4     });
5     console.log(array, num);      //[15, 5, 10, 2, 0] ,[45, 15, 30, 6, 0]

arr.filter (): to run on each item in the array of the given function, the function will return true return an array of items consisting of (Screening)

1     var array = [15, 5, 10, 2, 0];
2     var num = array.filter(function (value, index, array) {
3         return value >0;
4     });
5     console.log(array, num);         //[15, 5, 10, 2, 0] ,[15, 5, 10, 2]

Merge the two methods

  • arr.reduce (function (pre, next, index, array) {}) from left to right
  • arr.reduceRight (function (pre, next, index, array) {}) from right to left

some: to run on each item in the array of the given function, a function returns true if any, returns true, otherwise false

every: to run on each item in the array given function, if the function returns true for all items, only to return true, otherwise false

These are all the methods I know of an array, ES6 not related to the follow-up to add! !

String Methods

indexOf (data, start)    for returning an array or a string of characters or a string of predetermined positions; subscript current location of the query characters, if not, returns -1, start indicates the start queries from several.

1     var str="hello world!"
2     console.log(str.indexOf("h"));    //0
3     console.log(str.indexOf("i"));    //-1

str.lastIndexOf () determines a last character of a string appear in the index, if the index contains its return, or -1 if not included.

1     var str="hello world!"
2     console.log(str.lastIndexOf("d"));    // 10
3     console.log(str.lastIndexOf("i"));    //-1

str.charAt () Returns the position of character

1     var str="hello world!"
2     console.log(str.charAt(1));    //e
str.substr (n, m) starting from the index n, m characters taken, the intercepted return character, without any change to the original string
1     var str="hello world!"
2     console.log(str.substr(1));    //ello world!
3     console.log(str.substr(1,3))   //ell

str.substring (n, m) Returns the specified position n, to the end (free) of the string m, the end position is not specified if, from the start position to the end

1     var str="hello world!"
2     console.log(str.substring(1));    //ello world!
3     console.log(str.substring(1,3))   //el

str.slice (n, m)  with the substring, the array should be noted that the method and slice () is similar to

1     var str="hello world!"
2     console.log(str.slice(1));    //ello world!
3     console.log(str.slice(1,3))   //el

str.split ( "-") divided by the specified character string, returns an array

1     var str="hello world!"
2     console.log(str.split(" "));    // ["hello", "world!"]

str.replace ( "needs to be replaced character string", "character string after replacement") in a string some characters replaced with some other characters. Best with regular use

 
1     var str="hello world!"
2     console.log(str.replace("l","*"));    // he*lo world!

str.charCodeAt () Returns the index out of unicode characters

1     var str="hello world!"
2     console.log(str.charCodeAt(0));    // 104

str.concat () splice two strings and returns a new string, without any change to the original string.

1     var str="hello world!"
2     console.log(str.concat("d"));    // hello world!d
3     console.log(str);   //hello world!

str.match () to retrieve the value specified in string, or to find one or more regular expression matching. The characters found in the array, returns an array.

1     var str="hello world!"
2     console.log(str.match("d"));    // ["d", index: 10, input: "hello world!", groups: undefined]

str.search () method is used substring search character string specified, retrieving or regular expression matching substring.

1     var str="hello world!"
2     console.log(str.search("d"));    // 10
3     console.log(str.search("l"));   //2

The above is a string method, with an array does not involve the ES6! !

Note whether the method would affect the original array! !

 

 

Guess you like

Origin www.cnblogs.com/ruo-shui-yi-fang/p/11403995.html