Foreground interface (3) --- string and array, foreground ---

Source: Internet
Author: User

Foreground interface (3) --- string and array, foreground ---

Directory

1. String

1.1. Escape Sequence in a string

1.2. Immutable character string

1.3. String Index

1.4. string segmentation: split

1.5. concatenate arrays into strings: join

2. Array

2.1. Array Index

2.2. Multi-dimensional array

2.3. Array Method

2.3.1. data appended to the end of the array push () function

2.3.2. Remove the pop () function from the last element of the array.

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

2.3.4. Add an element unshift () function to the array Header

2.3.5. Iterative array: map

2.3.6. Add an array: reduce

2.3.7. filter Array: filter

2.3.8. array sorting: sort

2.3.9. Flip array: reverse

2.3.10. Merge Arrays: concat

----------------------- Golden line ----------------------1. String1.1. Escape Sequence in a string

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

Code

Output

\'

Single quotes

\"

Double quotation marks

\\

Backslash

\ N

Line Break

\ R

Carriage Return

\ T

Tab

\ B

Escape Character

\ F

Page feed

Note: If you want to display a backslash, You must escape it.

1.2. Immutable string

In JavaScript, the string value isImmutableThis means that once the string is created, it cannot be changed.

For example, the following code:

Var myStr = "Bob ";
MyStr [0] = "J ";

Does not 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 the string literalString literalCannot be changed. The only way to change the value in myStr is to re-assign it a value, just like this:

Var myStr = "Bob ";
MyStr = "Job ";

1.3. String Index

You can also use [Index] to obtain characters at other positions in a string.

Remember that the program starts counting from 0, so the first character obtained is actually [0].

To get the last character of a string, you can use [the length of the string minus one].

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

We can get the last character of a string or the nth character at the bottom of the string.

For example, you can perform the firstName [firstName. length-3] operation to obtain the last and third characters in the var firstName = "Charles" string.

1.4. String segmentation: Split

You can use the split method to split strings into arrays by specified separators.

You need to pass a parameter to the split method. This parameter will be used as a separator.

The following example shows how to use the split method, which is separated by the s letter:

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

Use the split method to split string into array arrays.

1.5. Concatenate arrays into strings: Join

We can also use the join method to convert an array into a string. Each element in it can be connected with a connector you specified. This connector is the parameter you want to pass in.

The following shows how to use join to put each item in the array into a string and connect it 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 into a string joinedString.

 

2. Array

Using arrays, we can store multiple data in one place.

[Start to define an array with square brackets] to end the definition, and separate each entry with a comma, just like this:
Var sandwich = ["peanut butter", "jelly", "bread"].

You can also include other arrays in the array, like this: [["Bulls", 23], ["White Sox", 45]. This is calledMulti-dimensional array.

2.1. Array Index

We can access the data in the array through the array index [index] Like the operation string.

The use of an array index is the same as that of a string index. The difference is that a character is obtained through a string index and an entry is obtained through an array index. Similar to strings, arrays areZero-basedSo the index of the first element of the array is 0.

Unlike the string data, the array data is variable and can be freely changed.

For example

Var ourArray = [3, 2, 1];
OurArray [0] = 1; // ourArray is equal to [1, 2, 1]

2.2. Multi-dimensional array

You can setMultidimensionalAn array is considered as an array in an array. When [] is used to access the array, the first [index] accesses the nth subarray, the second [index] accesses the nth element of the nth sub-array.

For example

Var arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[[10, 11, 12], 13, 14]
];
Arr [0]; // equals to [1, 2, 3]
Arr [1] [2]; // 6
Arr [3] [0] [1]; // equal to 11

2.3. Array Method2.3.1. Append data to the end of the array Push () Function

A simple method is to append data to the end of an array through the push () function.

. Push () accepts one or more parameters and pushes them to the end of the array.

Var arr = [1, 2, 3];
Arr. push (4 );
// The current arr value is [1, 2, 3, 4].

2.3.2. Removes the last element of the array. P Op () Function

Another way to change the data in the array is to use the. pop () function.

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

Any type of entries (numbers, strings, and even arrays) in the array can be "thrown out ".

For example
Var oneDown = [1, 4, 6]. pop ();
Now the value of oneDown is 6, and the array is changed to [1, 4].

2.3.3. Remove Array First element Shift () function

The pop () function is used to remove the last element from the array. What if I want to remove the first element?

This is the application of shift. It works like. pop (), but it removes the first element rather than the last one.

2.3.4. Add to the array Header Element Unshift () function

You can not only shift (remove) the first element in the array, but also unshift (move in) an element to the header of the array.

The. unshift () function is used just like the. push () function, but does not add elements at the end of the array, but adds elements to the header of the array.

2.3.5. Iteration array: Map

The map method can easily iterate arrays, for example:

Var timesFour = oldArray. map (function (val ){
Return val * 4;
});

The map method iterates every element in the array, processes each element based on the callback function, and returns a new array. Note that this method does not change the original array.

In our example, the callback function has only one parameter, namely the value of the element in the array (val parameter). However, your callback function can also support multiple parameters, for example: index of the element and the original array arr.

Use the map method to add 3 to each item in oldArray and save them in newArray. OldArray should not be changed.

2.3.6. Array accumulation: Reduce

The array method reduce is used to iterate an array and accumulate it into a value.

When using the reduce method, you need to input a callback function. The parameter of this callback function isAccumulators(For example, previusval In the example) and current value (currentVal ).

The reduce method has an optional second parameter, which can be used to set the initial values of the accumulators. If the initial value is not defined here, the initial value will become the first item in the array, and currentVal will start from the second item in the array.

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

Var singleVal = array. reduce (function (previusval, currentVal ){
Return previusval-currentVal;
}, 0 );

Use the reduce method to add all values in the array and assign the result to singleVal.

2.3.7. Filter Array: Filter

The filter method is used to iterate an array and filter the elements according to the given conditions.

The filter method is used to pass in a callback function. This callback function carries a parameter that is the item of the current iteration (we call it val ).

The items returned by the callback function to true are retained in the array, and items returned to false are filtered out.

The following code example shows how to use filter to remove an item with the value of 5 in the array:

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

Array = array. filter (function (val ){
Return val! = 5;
});

Use filter to create a new array. The value of the new array is an element whose value is less than 6 in oldArray. Do not change the original array oldArray.

2.3.8. Array sorting: Sort

With the sort method, you can easily sort the elements in the array in alphabetical or numerical order.

Unlike the array method we used to return only a new array, the sort method will change the original array and return the sorted array.

Sort can pass in a comparison function as a parameter. A comparison function returns a value. If a is less than B, a negative number is returned. If a is greater than B, a positive number is returned. If a is equal, 0 is returned.

If a comparison function is not input, it converts all values into strings and sorts them alphabetically.

The following example shows how to use sort. The input comparison function sorts the elements in ascending order:

Var array = [1, 12, 21, 2];
Array. sort (function (a, B ){
Return a-B;
});

Use sort to sort arrays in ascending order.

2.3.9. Flip array: Reverse

You can use the reverse method to flip the array.

Var myArray = [1, 2, 3];
MyArray. reverse ();

The result is that myArray is changed to [3, 2, 1].

Use reverse to flip the array. And assigned to newArray.

2.3.10. Merge Arrays: Concat

The concat method can be used to merge the content of two arrays into an array.

The concat method parameter should be an array. The array in the parameter is spliced behind the original array and returned as a new array.

The following is an example of concatenating arrays. Use concat to splice the otherArray to the end of oldArray:

NewArray = oldArray. concat (otherArray );

Use. concat () to splice concatMe to the end of oldArray and assign it to newArray.

Finally, let's talk about the important things three times. If you think they are good, you should recommend them or comment on them. If you think they are good, you may wish to continue to improve your suggestions, your support is my greatest encouragement.

Related Article

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.