Foreground interface (3)---strings and arrays

Source: Internet
Author: User

Directory

1. string

1.1. escape sequences in a string

1.2. non-variability of strings

1.3. String Index

1.4. string splitting:split

1.5. arrays concatenated into strings:join

2. Arrays

2.1. array index

2.2. Multidimensional Arrays

2.3. Array Methods

2.3.1. append data to the trailing push () function of the array

2.3.2. remove the last element of the divisor group pop () function

2.3.3. removing the first element of the array shift () function

2.3.4. Adding an element to the head of an array unshift () function

2.3.5. iterated algebraic groups:map

2.3.6. accumulation of arrays:reduce

2.3.7. filtering Arrays:filter

2.3.8. sorting by array:sort

2.3.9. flipping an array:reverse

2.3.10. Merging arrays:concat

-----------------------the golden dividing line ---------------------- 1. string1.1. escape sequences in a string

quotation marks are not the only characters in a string that can be escaped. The following is a list of common escape sequences :

Code

Output

\‘

Single quotation marks

\"

Double quotes

\\

Backslash character

\ n

Line break

\ r

Carriage return character

\ t

Tabs

\b

Backspace

\f

Page break

Note that if you want to display a backslash you have to escape it.

1.2. non-variability of strings

in JavaScript, the value of a string is immutable , which means that once a string is created it cannot be changed.

For example, the following code:

var mystr = "Bob";
Mystr[0] = "J";

is not to change the value of the variable mystr to "Job", because the variable mystr is immutable. Note that this does not mean that mystr can never be changed, but that the characters of string literal cannot be changed. the only way to change the mystr is to re-assign it a value, like this:

var mystr = "Bob";
MyStr = "Job";

1.3. String Index

You can also use Index to get a character from another position in the string.

keep in mind that the program is counted starting from 0, so getting the first character is actually [0].

in order to get the last character of a string, you can use the [Length of String minus one] .

For example, in var firstName = "Charles" , you can do this firstname[firstname.length-1] to get the last character of the string.

we can either get the last character of the string, or we can get the inverse of the string n characters.

For example, you can do this firstname[firstname.length-3] operation to get the third-to-last character in the var firstName = "Charles" string.

1.4. String Segmentation: Split

You can use the split method to split a string into arrays by a specified delimiter.

you are going to pass a parameter to the Split method, which will be used as a delimiter.

The following example shows the use of the split method, which is split by the S-Letter:

var array = string.split (' s ');

use the Split method to split string strings into arrays of array.

1.5. arrays are concatenated into strings: Join

we can also use the Join method to convert an array into a string, where each element can be concatenated with the connector you specify, which is the parameter you want to pass in.

The following shows the use of join to put each item in an array into a string and connect with and :

var veggies = ["celery", "radish", "carrot", "Potato"];
var salad = Veggies.join ("and");
Console.log (salad); "Celery and radish and carrot and Potato"

using the Join method, the connector converts the array joinme to a string joinedstring.

2. Array

Use Array , we can store multiple data in one place.

with the left parenthesis [ start defining an array with closing brackets ] end the definition and separate each entry with a comma, like this:
var sandwich = ["Peanut butter", "jelly", "bread"].

You can also include other arrays in the array, like this : [["Bulls", [[]], ["White Sox", []]. This is called a multidimensional array.

2.1. array index

we can pass the array index as if we were manipulating strings . [Index] to access the data in the array.

The use of an array index is the same as a string index, and the difference is that a character is obtained by the index of the string, and an entry is obtained through an array index. Like strings, arrays are also indexed based on 0 , so the index of the first element of the array is 0.

The data of a string is immutable, the data of the array is mutable, and can be changed freely.

For example

var ourarray = [3,2,1];
Ourarray[0] = 1; Ourarray equals [1,2,1]

2.2. Multidimensional Arrays

can put Multidimensional an array is considered as a Arrays in Arrays . When using [] to access an array, the first [index] accesses the nth sub-array, and the second [index] accesses the nth element of the nth sub-array.

For example

var arr = [
[A],
[4,5,6],
[7,8,9],
[[10,11,12], 13, 14]
];
ARR[0]; Equal to [a]
ARR[1][2]; equals 6
ARR[3][0][1]; equals 11

2.3. Array Method2.3.1. append data to the end of the array push () function

an easy way to append data to the end of an array is through the push () function.

The. push () accepts one or more parameters and pushes it into the end of the array.

var arr = [n/a];
Arr.push (4);
The value for arr now is [1,2,3,4]

2.3.2. move the last element of the divisor group P op () function

another way to change the data in an array is to use the. Pop () function.

The. Pop () function is used to "throw" a value at the end of an array. We can assign the value of this "throw" to a variable to store it.

any type of entry in the array (numeric, string, or even array) can be "thrown".

For example , for this piece of code
var onedown = [1, 4, 6].pop ();
now that the value of Onedown is 6 , the array becomes [1, 4].

2.3.3. Move out Array first element shift () function

The pop () function is used to move the last element in a group. What if you want to move out of the first element?

This is where shift () comes in. It works like . Pop (), but it removes the first element, not the last one.

2.3.4. added to the array header one Elements unshift () function

you can not only shift(move out) The first element in the array, you can also unshift(move in) an element to the head of the array.

The. Unshift () function is used just like the. push () function , but instead of adding elements at the end of the array, the element is added to the head of the array.

2.3.5. iterated algebraic groups: Map

The map method can be easily iterated by algebraic groups, for example:

var timesfour = Oldarray.map (function (val) {
Return Val * 4;
});

The map method iterates through each element in an algebraic group, processes each element according to the callback function, and finally returns a new array. Note that this method does not change the original array.

in our case, the callback function has only one argument, that is, the value of the element in the array (val parameter ), but in fact, your callback function can also support multiple parameters, such as: index of the element, theoriginal array arr.

use the map method to add 3 to each item in the Oldarray and save them in NewArray. Oldarray should not be changed.

2.3.6. accumulation of arrays: Reduce

Array Method reduce is used to iterate over an array and accumulate it into a value.

When using the reduce method, you pass in a callback function that has an accumulator (such as previousval in the example ) and the current value (currentval ).

the reduce method has an optional second parameter that can be used to set the initial value of the accumulator. If the initial value is not defined here, the initial value becomes the first item in the array, and Currentval begins with the second item in the array.

The following example uses reduce to subtract all the values in an array:

var singleval = array.reduce (function (Previousval, currentval) {
return previousval-currentval;
}, 0);

use the reduce method to add all the values in the array and assign the result to Singleval .

2.3.7. Filter Arrays: Filter

The filter method iterates over an array and filters out the conforming elements according to the given conditions.

The filter method passes in a callback function that carries a parameter that is the item of the current iteration (we call it Val ).

The callback function returns true if the item is persisted in the array, and the item that returns false is filtered by the group.

The following code example shows the use of filter to move an item in an array with a value equal to 5:

Note: we ignore the second and third parameters, because we only need the first parameter in the example to be sufficient.

Array = Array.filter (function (val) {
return Val!== 5;
});

using filter to create a new array, the value of the new array is an element with a value less than 6 in Oldarray. Don't change the original array Oldarray .

2.3.8. Array Sorting: Sort

using the sort method, you can easily sort the elements in an array in alphabetical order or numerically.

Unlike the array method we used earlier to simply return a new array, the sort method changes the original array and returns the sorted array.

Sort can pass the comparison function as a parameter. The comparison function has a return value, when A is less than b, a negative number is returned, and a number is returned when a is greater than b , and 0 is returned for equality .

If the comparison function is not passed in, it will turn the values all into strings and order alphabetically.

The following example shows the use of sort , and the incoming comparison function arranges the elements in order from small to large:

var array = [1, 12, 21, 2];
Array.Sort (function (A, b) {
return a-B;
});

use sort to sort the array in order from large to small .

2.3.9. To flip an array: Reverse

You can use the reverse method to flip an array.

var myArray = [1, 2, 3];
Myarray.reverse ();

The result MyArray became [3, 2, 1]

use reverse to flip array arrays. and assign a value to NewArray.

2.3.10. Merging arrays: concat

The Concat method can be used to merge the contents of a two array into an array.

the argument to the Concat method should be an array. The array in the parameter is stitched to the back of the original array and returned as a new array.

The following is an example of an array of stitching, using concat to stitch The Otherarray behind the Oldarray :

NewArray = Oldarray.concat (Otherarray);

use . Concat () to stitch the Concatme back into the Oldarray , and assign the value to NewArray.

Finally, finally, the important thing to say three times, is a guest, if you feel good on the recommendation or comment, feel bad hope to get your advice, continue to improve, your support is my greatest encouragement.

Foreground interface (3)---strings and arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.