Some methods arrays and strings summary

A. Array (Array)

1. Array Detection: isArray ()

New in ECMScript5 in the Array.isArray () method is used to confirm that a value is not an array.

2. Conversion Method: toString (), join ()

toString call array () method returns a string comma delimited string formed by combining each value obtained by the array.

Use join () method can use different delimiter to splice the value in the array into a string. join () method takes one parameter, i.e. as string delimiters.

var color = ['red', 'blue', 'green'];
alert(color.toString()); //red,blue,green
var color = ['red', 'blue', 'green'];
alert(color.join('|')); //red|blue|green

3. Method stack: push () pop ()

The latter is advanced out of the stack data structure.
push () method may receive any number of parameters, and add them to the end of the array by one, and returns the modified array length.
pop () method is to remove the last item from the end of the array, and replace the items removed.

4. The reordering method: reserve () short () 

reserve () method reverses the order of the array

 

var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values);  //5,4,3,2,1

 

sort (), arranged in an array entry method in ascending order by default. sort () method calls the toString () method, then the resulting string comparison, has determined how to sort each array item. Even if the array is an array each, sort () method is a string comparison.

 

var values = [0, 1, 5, 10, 15];
values.sort();
alert(values);  //0,1,10,15,5

To more accurately sort, Sort () method a comparison function may be received as an argument, the comparison function may receive two parameters, two parameters need to be exchanged if the position returns a positive number, without exchanging how returns negative, two parameters It is equal to 0 returns.

 

/升序
function compare(value1, value2) {
       if  (value1 < value2) {
              return -1;
       }  else if (value1 > value2) {
              return 1;
       } else {
              return 0;
       }
}
 
var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values);  //0,1,5,10,15

 

6, methods of operation: concat (), slice (), splice ()

(1) concat ()
to create a copy of the current array, and then add the received parameter to the end of this copy, and finally returns an array of new build. It does not change the terms of the original array.

var colors = ["red", "blue", "green"];
var colors2 = colors.concat("yellow", ["black", "brown"]);
 
alert(colors);  //red,blue,green
alert(colors2); //red,blue,green,yellow,black,brown

(2) slice ()
intercepting a plurality of items to create a new array from the current array. Receive one or two parameters, i.e., to return to the start and end position of the item, but the item does not include the end position. If only one parameter, then all of the item from the starting position to the end of the array.

If the parameter slice () is in a negative number, determining a position corresponding to the length of the array plus the number. If the ending position is less than the starting position, it returns an empty array.

var colors = ["red", "blue", "green", "black", "brown"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1, 4);
 
alert(colors2);  //blue,green,black,brown
alert(colors3); //blue,green,black

(. 3) splice ()
splice () method may delete entry array, substitution, insertion operation.
Delete: you need to specify two parameters, the first item you want to delete the location and number of items to be deleted. The splice (1, 2);
Insert: three parameters specify the starting position, 0 (the number of items to be removed) and the items to be inserted. If you want to insert more than one item, you can continue to pass the 4th, 5th, ..., n-th item.
Alternatively: three parameters specify the starting position, the number of items to be inserted and any number of entries to be removed. You can insert any number of items to the specified location and delete any number of items.

splice () method always returns an array that contains the items removed from the original array, without the deleted items, it returns an empty array.

//删除
var colors = ["red", "blue", "green"];
var removed = colors.splice(0,1);
alert(colors);  //blue,green
alert(removed);  //red
 
//插入
removed = colors.splice(1, 0, "black", "brown");
alert(colors);  //blue,black,brown,green
alert(removed);  //[]
 
//替换
removed = colors.splice(1, 1, "red", "purple");
alert(colors);  //blue,red,purple,brown,green
alert(removed);  //black

7, the position of the method: indexOf (), lastIndexOf ()

Both methods can receive two parameters: To find items and represents the starting point of the index to find the location (optional). The return value is the position you want to find items in the array, if not found, -1 is returned.
indexOf (): front look back
lastIndexOf (): Find from back to front

8, an iterative method: every (), some (), filter (), forEach (), map ()

every () and some () are used to query the array item meets certain conditions, every () when passed into the function of each item are in line with returns true, while some () as long as there is a satisfying return true.

filter () returns an array qualifying array item.

map () function returns an array of running passed the results on each item of the original function.

forEach () operation for each incoming array function returns no value.

9, merge method: reduce (), reduceRight ()
all iterations of the array, and then construct a return value. reduce () starting from the first item in the array, one by one to the last traverse, reduceRight () is from the last item of the array, traversing forward to the first item.
Both methods accepts two parameters, a function is called on each of the second as a basis for merging an initial value (optional). Incoming function may receive four parameters: the previous value, the current value of the index, and an array of object entry. So the first iteration is the second place in the array.

Second, the String (String)

1、charAt()和charCodeAt()

Receives a parameter, i.e., based on the character position 0.
charAt (): returns the string of characters as a single given position of the character.
charCodeAt (): returns the given position of character code character.

2、concat()和加号操作符(+)
concat()用于将一个或多个字符串拼接起来,返回拼接得到的新字符串。但是实际操作中用到更多的是加号操作符(+),更加的简便易行。

3、slice()、substr()、substring()
这三个方法都会返回被操作字符串的一个子字符串。可以接收两个参数,用来表示子字符串的开始位置和结束位置。

slice()和substring()第二个参数指定的是子字符串最后一个字符后面的位置,也就是截取的子字符串中不包含该位置的字符。
substr()第二个参数指定的是子字符串的长度。

如果给的参数是负数:

slice() 会将传入的负值与字符串的长度相加;
substr() 会将负的第一个参数加上字符串的长度,而将第二个负的参数转换为0;
substring() 会将所有的负值参数都转换为0。会自动将小的参数作为开始位置,大的参数作为结束位置。

4、indexOf()和lastIndexOf()

与数组中的位置方法相同。这两个方法都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到,则返回-1。

5、trim()

该方法会创建一个字符串的副本,删除前置和后缀的所有空格。

6、大小写转换:toLowerCase()和toUpperCase()

7、模式匹配方法:match()、search()、replace()、split()

match() 与RegExp的exec()方法相同。只接收一个参数,正则表达式或者RegExp对象。

var text = "cat, bat, hat, fat";
var pattern = /.at/;
 
var matches = text.match(pattern);
alert(matches.index);  //0
alert(matches[0]);  //"cat"
alert(pattern.lastIndex);  //0

 

search() 返回字符串中第一个匹配项的索引,如果没有找到匹配项则返回-1。

replace() 接收两个参数,第一个是RegExp对象或者一个字符串,第二个参数可以是一个字符串或者函数。如果第一个参数是字符串,则只会替换第一个子字符串,要想全部替换,则只有提供一个正则表达式,并且制定全局(g)标志。

replace() 方法第二个参数可以是一个函数。函数中传递三个参数:模式的匹配项(一个或者多个)、模式匹配项在字符串中的位置和原始字符串。

function htmlEscape(text) {
    return text.replace(/[<>"&]/g, function(match, pos, originText) {
        switch(match) {
            case "<":
                return "&lt";
            case ">":
                return "&gt";
            case "&":
                return "&amp;";
            case "\"":
                return "&quot";
        }
    });
}
 
alert(htmlEscape("<p class=\"greeting\">hello world!</p>"));  //&lt;p class=&quot;greeting&quot;&gt;hello world!&lt;/p&gt;

 

split() 基于制定的分隔符将一个字符串分割成多个子字符串,并将结果放在一个数组中。与数组的join()方法相反。split()可以接收可选的第二个参数,用于制定数组的大小。

 

Guess you like

Origin www.cnblogs.com/xcyzkh/p/11305431.html