JavaScript array additions and deletions change sorting string mutual transfer

Source: Internet
Author: User

Arr. arrays
New array
STR-strings
Modify Location Index--
Modified Quantity N
Item-Modified Content

"1" Array additions
1, index in turn increase
Arr.push (' item ');
Put item
2. Start adding ' item ' at the specified index
Arr.splice (index, 0, ' item ');
The second parameter must be a 0!!!

"2" removes the element from the array
1. Delete all elements from index, including index
Arr.splice (Index)
2. Delete the index element, followed by n elements, including index
Arr.splice (index, n);

"3" modifies array elements
1. Item must not be empty
Arr.splice (index, N, ' item ');
Replace the N element after index start with item, including index;

"4" finds an element in an array
1, know ' subscript ' get ' value '
var element = Arr[index];
2, know ' value ' get ' subscript '
var index = $.inarray (Element,arr);
If index =-1 indicates that ARR is not an array

"5" Array to string
1. The join () method composes all the elements of an array into a string
var arr = [A, B, C, D, E];
var str = arr.join (', ');
Use ', ' as separators;
Result: str = ' ABCDE '

"6" string to array
1. The string is cut into several strings by a character and returned as an array
var str = ' a,b,c,d,e ';
var arr = Str.splice (', ');
Result: arr = [A, B, C, D, E]

"7" Sequential sorting
1. Sort ()
var arr = [A, D, C, E, b];
var new = Arr.sort ();
Result: New = [A, B, C, D, E]

"8" random sort
1. Sort (function () {return 0.5-math.random ()})
var arr = [A, B, C, D, E];
var new = Arr.sort (function () {return 0.5-math.random ()});
Results: new = [A, D, C, E, b];
Results: new = [C, D, A, E, b];
...

JavaScript array additions and deletions change sorting string mutual transfer

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.