Comparison of array and string methods in javascript, javascript Array
Previous
Strings have many similarities with arrays. They have many methods and have a high similarity. However, they differ that strings are immutable values, so they can be viewed as read-only arrays. This document compares the methods similar to strings and arrays.
Indexable
ECMAScript5 defines an access character method that uses square brackets and numerical indexes to access specific characters in a string.
The biggest benefit of an indexed string is simplicity. It is called with square brackets instead of charAt (), which is more concise, readable, and possibly more efficient. In addition, the string behavior is similar to the fact that the general array method can be applied to the string.
If the parameter is out of the range or NaN, the output is undefined.
Var str = "hello"; console. log (str [0]); // hconsole. log (str [[1]); // econsole. log (str [false]); // undefinedconsole. log (str [-1]); // undefinedconsole. log (str [NaN]); // undefinedconsole. log (str []); // Error
Var arr = ['h', 'E', 'l', 'l', 'O']; console. log (arr [0]); // hconsole. log (arr [[1]); // econsole. log (arr [false]); // undefinedconsole. log (arr [-1]); // undefinedconsole. log (arr [NaN]); // undefinedconsole. log (arr []); // Error
Conversion
The split () method can be used to convert a string to an array, while the join () method can be used to convert an array to a string.
[Split ()]
The split () method Splits a string into multiple strings Based on the specified delimiter and places the results in an array. The delimiter can be a string or a regular expression.
This method can accept (optional) The second parameter is used to specify the size of the array. If the second parameter is a value in the range of 0-array.length, all results are output according to the specified parameter.
If the specified separator does not appear in the string, return the value of the original string in the form of an array.
Var colorText = 'Red, blue, green, yellow'; console. log (colorText. split (''); // [" r "," e "," d "," B "," l "," u ", "e", "g", "r", "e", "e", "n", "," y "," e ", "l", "l", "o", "w"] console. log (colorText. split (','); // ["red", "blue", "green", "yellow"] console. log (colorText. split (',', 2); // ["red", "blue"] console. log (colorText. split (',', 6); // ["red", "blue", "green", "yellow"] console. log (colorText. split ('-'); // ["red, blue, green, yellow"] console. log (colorText. split (/\,/); // ["red", "blue", "green", "yellow"] console. log (colorText. split (/e/); // ["r", "d, blu", ", gr", "", "n, y ", "llow"] console. log (colorText. split (/[^ \,] +/); // convert strings other than commas into separators ["",",",",",",", ""], IE8-will be recognized as [","]
[Join ()]
The join () method can use different delimiters to construct this string. The join () method only receives one parameter and serves as a separator string. Then, it returns a string containing all array items.
If no value is input to the join () method, use a comma as the separator.
Var a = [1, 2, 3]; console. log (. join (); // '1, 2, 3 'console. log (. join (''); // '1 2 3' console. log (. join (''); // '000000' var B = new Array (10); B. join ('-'); // '--------- ', a string consisting of 9 Characters
If the value of an item in the array is null or undefined, the value is expressed as an empty string in the result returned by the join () method.
var colors = [1,undefined,2,null,3];console.log(colors.join());//'1,,2,,3'
Because the string is an array-like object, you can also use the join () method.
console.log(Array.prototype.join.call('hello', '-'));// "h-e-l-l-o"
var str = 'test';var arr = str.split('')//["t", "e", "s", "t"]console.log(arr.join('-'));//'t-e-s-t'
Splicing
The concatenation method concat () is available for strings and arrays ()
var value = 'hello';console.log(value.concat('world'));//'helloworld'console.log(value.concat(['world']));//'helloworld'console.log(value.concat([['world']]));//'helloworld'
var value = ['hello'];console.log(value.concat('world'));//["hello", "world"]console.log(value.concat(['world']));//["hello", "world"]console.log(value.concat([['world']]));//["hello", ["world"]]
Create
Both the string and array have the creation method slice (), which is used to create the substring and the subarray respectively.
The slice () method creates a new array (or string) based on one or more items in the current array (or string) and accepts one or two parameters, that is, the start and end positions of the items to be returned, and the new array (or string) is returned)
The slice (start, end) method requires two parameters, start and end, and returns the Array (or string) from the start position to (but not included) A subarray (or string) at the end position. If the end is undefined or does not exist, all items from the start position to the end of the array (or string) are returned.
If start is negative, start = max (length + start, 0)
If end is negative, end = max (length + end, 0)
The start and end cannot be switched.
var numbers = [1,2,3,4,5];console.log(numbers.slice(2));//[3,4,5]console.log(numbers.slice(2,undefined));//[3,4,5]console.log(numbers.slice(2,3));//[3]console.log(numbers.slice(2,1));//[]console.log(numbers.slice(-3));//-3+5=2 -> [3,4,5]console.log(numbers.slice(-8));//max(5 + -8,0)=0 -> [1,2,3,4,5]console.log(numbers.slice(0,-3));//-3+5=2 -> [1,2]console.log(numbers.slice(-2,-1));//-2+5=3;-1+5=4; -> [4]
var stringValue = 'hello world';console.log(stringValue.slice());//'hello world'console.log(stringValue.slice(2));//'llo world'console.log(stringValue.slice(20));//''console.log(stringValue.slice(2,undefined));//'llo world'console.log(stringValue.slice(2,-5));//'llo 'console.log(stringValue.slice(2,-20));//''console.log(stringValue.slice(-2,2));//''console.log(stringValue.slice(-2,-20));//'' console.log(stringValue.slice(-2,20));//'ld'console.log(stringValue.slice(-20,2));//'he'console.log(stringValue.slice(-20,-2));//'hello wor'
Location
Both the string and array have two methods to find the location: indexOf () and lastIndexOf (). The location method is the opposite of the [] reading method in brackets. One is to search for an index by item, and the other is to search for an index by index.
[IndexOf ()]
The indexOf (search, start) method receives the search and start parameters and returns the location where the search first appeared. If not found, returns-1.
The search parameter in the String calls the String () transformation function to convert the non-String value of this parameter to a String. The search parameter in the array is compared using the strictly equal operator (= ).
The second parameter, whether it is an array or a string, implicitly calls the Number () transformation function to convert a non-numeric start value (except undefined) to a value; if this parameter is ignored or the parameter is undefined or NaN, start = 0
If the start parameter is a negative number, the string is processed with start = 0, while the array is processed with start = max (0, start + length)
var string = 'hello world world';console.log(string.indexOf('ld'));//9console.log(string.indexOf('ld',undefined));//9console.log(string.indexOf('ld',NaN));//9console.log(string.indexOf('ld',-1));//9console.log(string.indexOf('ld',10));//15console.log(string.indexOf('ld',[10]));//15console.log(string.indexOf('true',[10]));//-1console.log(string.indexOf(false,[10]));//-1
var arr = ['a','b','c','d','e','a','b'];console.log(arr.indexOf('a',undefined));//0console.log(arr.indexOf('a',NaN));//0console.log(arr.indexOf('a',1));//5console.log(arr.indexOf('a',true));//5console.log(arr.indexOf('a',-1));//max(0,-1+7)=6; -1console.log(arr.indexOf('a',-5));//max(0,-5+7)=2; 5console.log(arr.indexOf('a',-50));//max(0,-50+7)=0; 0
[LastIndexOf ()]
Opposite to the indexOf () method, the lastIndexOf () method is used to search from right to left.
The lastIndexOf (search, start) method receives the search and start parameters, returns the location where the searchString first appeared, and returns-1 if not found.
Similarly, the search parameter in a String calls the String () transformation function to convert the non-String value of this parameter to a String; the search parameter in the array is compared using the strictly equal operator (= ).
The second parameter, whether it is an array or a string, implicitly calls the Number () transformation function to convert a non-numeric start value (except undefined) to a numeric value.
If this parameter is ignored or the parameter is undefined or NaN, the string processing is start = length-1, while the Array Processing is start = 0.
If the start parameter is a negative number, the string is processed with start = 0, while the array is processed with start = max (0, start + length)
var string = 'hello world world';console.log(string.lastIndexOf('ld'));//15console.log(string.lastIndexOf('ld',undefined));//15console.log(string.lastIndexOf('ld',NaN));//15console.log(string.lastIndexOf('ld',-1));//-1console.log(string.lastIndexOf('h',-1));//0console.log(string.lastIndexOf('w',undefined));//12console.log(string.lastIndexOf('ld',10));//9console.log(string.lastIndexOf('ld',[10]));//9console.log(string.lastIndexOf('true',[10]));//-1console.log(string.lastIndexOf(false,[10]));//-1
var arr = [1,2,3,'1','2','3'];console.log(arr.lastIndexOf('2'));//4console.log(arr.lastIndexOf(3));//2console.log(arr.lastIndexOf(0));//-1var arr = ['a','b','c','d','e','a','b'];console.log(arr.lastIndexOf('b'));//6console.log(arr.lastIndexOf('b',undefined));//-1console.log(arr.lastIndexOf('a',undefined));//0console.log(arr.lastIndexOf('b',NaN));//-1console.log(arr.lastIndexOf('b',1));//1console.log(arr.lastIndexOf('b',-1));//max(0,-1+7)=6; 6console.log(arr.lastIndexOf('b',-5));//max(0,-5+7)=2; 1console.log(arr.lastIndexOf('b',-50));//max(0,-50+7)=0; -1
The comparison of arrays and strings in the above javascript text is all the content shared by the editor. I hope to give you a reference and support for the help house.