Javascript array summary JS array method summary using call Methods
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 alw
function needs to be converted to a string. Seeing ToString () Here, I remember the implicit invocation of ToString () and valueof (). In a less rigorous sense, valueof () is implicitly called when calculations are required. ToString () is called when you need to display data or results in a string environment. Interested students can Google a bit.3. Create a new array from an existing array:The Concat () method merges multiple existing arrays, creating a new arrayThe
An array is a linear memory allocated. It uses Integers to calculate the offset and access the elements. Arrays are fast data structures, but unfortunately Javascript does not have the same data structure as this array. The array of Javascript is actually an object. It converts the subscript of the array into a string and uses it as an attribute. Therefore, it is obviously slower than the real array,
An array is a linear memory allocated. It uses Integers to calculate the offset and access the e
, 4, 5];Var B = a. unshift (-2,-1); // a: [-2,-,] B: 7Note: 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 =
1. Add the push method of array elements to add the elements at the end of the array: varcolorArraynewArray (); colorArray. push (red, black, yellow); // It is directly pushed into three elements. // Of course, you can also write colorArray. push (red); colorArray. push (black); 2. Deletion of array elements
1. Add array elements
The push method adds elements at the end of the array:
Var colorArray = new Array ();
ColorArray. push ('red', 'black', 'yellow'); // press the three elements directly.
, return undefined var a = [, 5]; var B =. shift (); // a: [2, 3, 4, 5] B: 1 unshift: add the parameter to the beginning of the original array, and return the array length var a = [1, 2, 3, 4, 5]; var B =. unshift (-2,-1); // a: [-2,-,] B: 7 Note: in IE6.0, the returned value is always undefined, the test return value in FF2.0 is 7. Therefore, the return value of this method is not reliable and must be replaced by splice when the return value is used.
inside the arrayvar arr = [1,2,3,4,5];arr.shift (); Console.log (arr); The result is [2,3,4,5]6. Array of splice methods: used to cut the elements of the group, and can be added and modified operationvar Namearr = ["Yangyang", "Fang Fang", "Round Round", "xiaoming"];//when splice has only one parameter n splice (n), only the first n elements of the array are pre
href = window. location. href; // The complete URL of the website (Protocol + domain name + parameter + anchor) var hostPath; // The string used to store the parameters removed from the website (Protocol + domain name + anchor) var urlPath; // an array used to store the URL path of a website (for example, test.cn/test/xss/123 urlPath = ['test', 'xss', '123'])
Variable operation:
If (href. indexOf ("? ")! = "-1") {// If the url exists? String (exist? Basically there is a parameter.) hostPath = h
This article provides a detailed analysis of the addition and deletion of javascript arrays and json elements. For more information, see
Three methods for deleting arrays by using explain script1. Use shift () methodShift: 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 chaomao = [1, 2, 3, 4, 5]
Var chaomao. shift () // get 1
Alert (chaomao) // [2, 3, 4, 5]
2. Use the pop () methodPop: Delete the last en
Shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, return undefined var a = [1, 2, 3, 4, 5]; var B =. shift (); // a: [2, 3, 4, 5] B: 1 unshift: add the parameter to the beginning of the original array, and return the array length var a = [1, 2, 3, 4, 5]; var B =. unshift (-2,-1); // a: [-2,-,] B: 7 Note: In IE6.0, the returned value is always undefined, the test return value in FF2.0 is 7, so the return value of this method is
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// 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:
the array items are compared according to the character code, 1 is less than 2, so 15 is in front of 2 .. The solution is as follows: var acolors = ["15", "2", "3"];Acolors. Sort ();Alert (acolors. tostring (); // output, 3
VaR acolors = ["15", "2", "3"];Acolors. Sort (compareintegers );Alert (acolors. tostring (); // output 2, 3, 15
Function compareintegers (vnum1, vnum2 ){VaR vnum1 = parseint (vnum1 );VaR vnum2 = parseint (vnum2 );If (vnum1 Return-1;} Else if (vnum1> vnum2 ){Return 1;} Else {
ArticleDirectory
Array object Method
Attribute of an array object
An array is a linear memory allocated. It uses Integers to calculate the offset and access the elements. Arrays are fast data structures, but unfortunately JavaScript does not have the same data structure as this array. The array of JavaScript is actually an object. It converts the subscript of the array into a string and uses it as an attribute. Therefore, it is obviously slower than the real array, but it can
Array. prototype. push
Push adds an item to the end of the array and updates the length to return the array length.
What if the Object uses push?
Look at the following code. obj works like an array. Length is automatically updated.
Copy codeThe Code is as follows: var push = Array. prototype. push;
Var obj = {};
Push. call (obj, "hello"); // return value 1
// Obj {"0": "hello", length: 0}
Push. call (obj, "world"); // return value 2
// Obj {"0": "hello", "1": "world", length: 2}
Array. prototyp
() method to sort the members in the array (in alphabetical order by default ). Like the reverse () Statement, the sort () Statement modifies the array itself and returns the modified array object:
Copy codeThe Code is as follows:Var a = ["Phone", "Mobile", "Canon"];A. sort ();Console. log (a); // ["Canon", "Mobile", "Phone", undefined, undefined]Var B = [33, 44, 111];Console. log (B. sort (); // [111, 33, 44]Console. log (B. sort (function (a, B) {return a-B}); // [33, 44,111]
As you can see,
. Ultimate artifact
JavaScript provides a splice Method for adding and deleting arrays at a time (the two methods can be used together to achieve the replacement effect). The method has three parameters.
1. Start Indexing
2. Delete element displacement
3. You can also write multiple new elements.
The splice method returns a new array composed of deletion elements. If no elements are deleted, an empty array
Increase push (); Add an element to the end of an array unshift (); Adding elements to an array headerTo add an element to an array by specifying the subscript:The splice () can be provided with array;var arr = [' A ', ' B ', ' C '];arr.splice (1,0, ' e '); arr[1]; // return e the array prototype chain supports extensions and can add a new function. //array prototype chain to add a new function. function(index,val) { if(Index >-1) {
program after the contents of the memory segment is modified, and it can be set to a bitwise OR of certain values (where map_shared and map_privateis mutually exclusive and cannot be specified at the same time). Common values for the flags parameter are: map_shared, Map_private, map_anonymous, map_fixed, MAP_hugetlb. The FD parameter is the file descriptor corresponding to the mapped file, which is usually obtained by the open system call. The offset parameter is set from the fileStart mapping
: The JavaScript interpreter only allocates memory to the elements whose subscript is 9, and no other subscript exists.Var array = [];Array. length = 10; // Add the length of the array;Array [array. length] = 4;/* Delete array elements */// The delete operator sets an array element to the undefined value, but the element still exists.// Delete the object. You can use Array. shift (); [Delete the first entry] Array. pop (); [Delete the last one] Array. splice
Defines and uses the splice () method to insert, delete, or replace elements in an array. The arrayObject. splice (index, howmany, element1,..., elementX) parameter describes the index required. Specifies where to add/delete elements. This parameter is the number of start inserts and/or deletes... Syntax
Definition and usageThe splice () method is used to insert,
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.