(); // 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, in FF2.0, the test return value is 7, so The return value 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 Array. observe () method is used to asynchronously monitor changes in arrays, similar to Object. observe () for objects (). When the value of the array changes, it provides a change stream in the order of occurrence. Similar to Object. observe (), it is triggered by the following list of acceptable change types: [add, update, delete, and splice. The javascript Array. observe () method is used to asynchronously monitor changes in arrays.
Observe (
: the returned array contains the element specified by the first parameter and the element starting from the element specified by the second parameter, but does not contain the element specified by the second parameter.
document.write("h.slice(4,5)="+h.slice(4,5));document.write("h.slice(5,9)="+h.slice(5,9))
The splice () method is a common method for inserting or deleting array elements. The first parameter of the
, 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: 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.
excluding the entry at the end position NBSP; Span style= "FONT-SIZE:14PX; Color: #000000; " >ARR1 = [1,2,3,4,5]; result = Arr1.slice (2,5);//[3,4,5] result = Arr1.slice (2); [3,4,5] In the case of only one argument, the slice () method returns all items starting at the specified position of the parameter to the end of the current array;
no
8
Splice Delete or replace some items of the current array, affecting the ori
return the length of the array.Var a = [1, 2, 3, 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, undef
Split converts the string into an array and outputs the Code:
Copy codeThe Code is as follows:
Js deletes array elements:
Var arr = ['A', 'B', 'C'];To delete 'B', you can use either of the following methods:
1. delete method: delete arr [1]
In this way, the length of the array remains unchanged. In this case, the arr [1] is changed to undefined, but the index of the original array remains unchanged. In this case, you need to traverse the array element before using it.Copy codeThe Code is as foll
))
{
From = len-1;
}
Else
{
From = (from ? Math. ceil (from)
: Math. floor (from );
If (from From + = len;
Else if (from> = len)
From = len-1;
}
For (; from>-1; from --)
{
If (from in this
This [from] === elt)
Return from;
}
Return-1;
};
}
Array. prototype. insertAt = function ($ value, $ index)
{
If ($ index This. unshift ($ value );
Else if ($ index> = this. length)
This. push ($ value );
Else
This.
Summary of several methods for deleting arrays in js
This article mainly summarizes several methods for deleting arrays in js. For more information, see.
Var arr = ['A', 'B', 'C'];
To delete 'B', you can use either of the following methods:
1. delete method: delete arr [1]
In this way, the length of the array remains unchanged. In this case, the arr [1] is changed to undefined, but the index of the original array remains unchanged. In this case, you need to traverse the array element before u
yesterday, a scene like This: there is a non-paged list of goods, there may be thousands of data (and possibly static Data) and even more, there is a delete function, we need to delete some of these items.My first reaction was that too much data could not loop through the entire array, only to get to their index, and then iterate over the index, using the Array's splice () method to delete it. And I did, but I found a fatal Bug.The reason for the bug
): splice () method:First, he and slice have only one letter difference, but the use is completely different.It can be used to delete objects.VaR A = [1, 2, 3];A = A. splice (0, 2 );Alert (a); // output 1, 2A = A. splice (1, 2 );Alert (a); // Output 2. If it is a = A. splice (0, 1); Output 1A = A.
Array---Common properties and methods summary 1, Array object constructor1 /*Array Object Constructors*/2 3 /*Combination Memory shift unshift POP push4 Add and remove5 shift Unshift from the beginning of the array to add or remove6 pop push is added or removed from the end of the array7 */8 9 //Shift: Deletes the first item of the original array and returns the value of the deleted element; returns undefined if the array is emptyTen varARR1 = [1,2,3,4,5]; One varARR2 = [1,2
Colors.pop () (removed from the back)(6) Queue method (FIFO) FIFO: Colors.unshift ("Red", "green" ...) (add in front) and Colors.unshift () (delete from front)(7) Reflow method: Reverse () and sort ()Example 1:var values = [1,2,3,4,5] 'Values.reverse ();alert (values);//5,4,3,2,1Example 2:var values = [0,1,5,10,15] 'Values.sort ();alert (values);//0,1,10,15,5 because sort does not pass parameters, it is arranged by stringExample 2:function Compare (value1,value2) {if (value1 return-1;}else if (
array does not change.For detailed syntax, please refer to: SliceSpliceAlso we can use splice() the method to select, as follows:var aPerson = [‘person1‘, ‘person2‘, ‘person3‘, ‘person4‘, ‘person5‘, ‘person6‘]var aP3 = aPerson.splice(1, 3);console.log(aPerson); // ["person1", "person5", "person6"]console.log(aP3); // ["person2", "person3", "person4"]The method changes the contents of an array by deleting an existing element or adding a new element. T
then returns the removed item(4) Shift () method: Move the first item in the array and return the change, minus 1(5) Unshift () method: Add any item to the front of the array and return the length of the new array(6) Sort () method: Sort data in an array(7) Concat () method: You can create a new array based on all the items in the current array(8) Slice () method: Creates a new array based on one or more items in the current array(9) IndexOf () method: Look backwards from the beginning of the a
var arr=[' A ', ' B ', ' C '];To remove the ' B ', there are two ways: 1.delete method: Delete Arr[1]This way the array length is not changed, at this point arr[1] becomes undefined, but also has the benefit of the original array index also remains unchanged, at this time to repeat the group elements can be used For (index in ARR){document.write (' arr[' +index+ ']= ' +arr[index]);} This traversal way skips the elements of undefined* This method IE4.O support after all. 2. Array Object
var arr=[' A ', ' B ', ' C '];There are two ways to delete a ' B ' in it:
1.delete method: Delete Arr[1]
This way the array length does not change, at this time arr[1] becomes undefined, but also has the advantage the index of the original array also remains unchanged, at this time to enumerate the elements of the group can be used
Copy Code code as follows:
For (index in ARR)
{
document.write (' arr[' +index+ ']= ' +arr[index]);
}
This traversal way skips over the
array, adding items to the end of the array without affecting the original array:
var colors=["Red", "green", "Blue"];
var colors2=color.contact ("Pink", "black");
var myarr1= new Array ("010")
var myarr2= new Array ("-", "84697581");
document.write (Myarr1.concat (MYARR2));
(2) Slice () creates a new array based on one or more items in the current array, accepting one or two arguments, that is, to return the starting and ending positions of the items:
var color3=colors.slice (1,2);
(3)
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 be used more conveniently.
Change your own pop, push, reverse, shift, sort,
Shift: Delete the first entry of the original array, and return the value of the deleted element. If the array is empty, undefinedvara [, 5]; varba is returned. shift (); Result 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 vara... syntaxHighlighte
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 =
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.