Array usage in JS {reproduced}

Source: Internet
Author: User
The addition and deletion of js array elements have been confusing. Today, I finally found detailed information and gave me the code to test. ^-^

The addition and deletion of js array elements have been confusing. Today, I finally found detailed information and gave me the code to test. ^-^
Var arr = new Array ();
Arr [0] = "aaa ";
Arr [1] = "bbb ";
Arr [2] = "ccc ";
// Alert (arr. length); // 3
Arr. pop ();
// Alert (arr. length); // 2
// Alert (arr [arr. length-1]); // bbb
Arr. pop ();
// Alert (arr [arr. length-1]); // aaa
// Alert (arr. length); // 1

Var arr2 = new Array ();
// Alert (arr2.length); // 0
Arr2 [0] = "aaa ";
Arr2 [1] = "bbb ";
// Alert (arr2.length); // 2
Arr2.pop ();
// Alert (arr2.length); // 1
Arr2 = arr2.slice (0, arr2.length-1 );
// Alert (arr2.length); // 0
Arr2 [0] = "aaa ";
Arr2 [1] = "bbb ";
Arr2 [2] = "ccc ";
Arr2 = arr2.slice (0, 1 );
Alert (arr2.length); // 1
Alert (arr2 [0]); // aaa
Alert (arr2 [1]); // undefined

Shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. shift (); // a: [2, 3, 4, 5] B: 1

Unshift: add the parameter to the beginning of the original array and return the length of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. unshift (-2,-1); // a: [-2,-,] B: 7
Note: In IE6.0, the test return value is always undefined, and in FF2.0, the test return value is 7. Therefore, the return value of this method is unreliable. You need to use splice instead of this method when returning the value.

Pop: Delete the last entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. pop (); // a: [1, 2, 3, 4] B: 5 // you can directly call it without returning it.

Push: add the parameter to the end of the original array and return the length of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. push (6, 7); // a: [1, 2, 3, 4, 5, 6, 7] B: 7

Concat: returns a new array consisting of adding parameters to the original array.
Var a = [1, 2, 3, 4, 5];
Var B = a. concat (6, 7); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5, 7]

Splice (start, deleteCount, val1, val2,...): Delete the deleteCount item from the start position, and insert val1, val2 ,...

When clearing the array, you only need to pass startIndex.

If you do not delete all elements, pass the deleteCount parameter.

Splice also has the ability to delete and then add, that is, delete several elements first, and then add several elements at the location of the deletion. The number of deleted elements is not equal to the number of added elements, deleteCount is also used at this time.
Var a = [1, 2, 3, 4, 5];
Var B = a. splice (, 9); // a: [,] B: []
Var B = a. splice (0, 1); // same as shift
A. splice (0, 0,-2,-1); var B = a. length; // same as unshift
Var B = a. splice (a. length-1, 1); // same as pop
A. splice (a. length, 7); var B = a. length; // same as push

Reverse: returns the reverse order of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1]

Sort (orderfunction): sorts arrays by specified parameters.
Var a = [1, 2, 3, 4, 5];
Var B = a. sort (); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5]

Slice (start, end): returns a new array consisting of items from the original array that specify the start subscript to the end subscript
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (); // a: [, 5] B: [, 5]

Join (separator): A string is set up for the elements of the array. The separator is separator. If it is omitted, a comma is used as the separator by default.
Var a = [1, 2, 3, 4, 5];
Var B = a. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"

Then we can use an array to simulate javaStringBuffer to process strings:

/**
* String processing functions
*/
Function StringBuffer (){
Var arr = new Array;
This. append = function (str ){
Arr [arr. length] = str;
};

This. toString = function (){
Return arr. join (""); // ping the append array to a string.
};
}

Today, in the application, we suddenly found that join is a good way to convert arrays into strings, so it is encapsulated into objects using:

/**
* Convert an array to a string separated by a specific symbol.
*/
Function arrayToString (arr, separator ){
If (! Separator) separator = ""; // If separator is null, the default value is null.
Return arr. join (separator );
}

/**
* Search for strings contained in an array
*/
Function arrayFindString (arr, string ){
Var str = arr. join ("");
Return str. indexOf (string );
}

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.