This article mainly shares with you the analysis of js array operation examples, mainly in the form of code, hoping to help everyone.
shift: Delete the first item 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 = a.shift(); //a:[2,3,4,5] b:1
unshift: Add parameters 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,-1,1,2,3,4,5] b:7
Note: The test return value under IE6.0 is always undefined, and the test return value under FF2.0 is 7, so the return value of this method is unreliable. When you need to use the return value, splice can be used instead of this method.
pop: Delete the last item 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 = a.pop(); //a:[1,2,3,4] b:5
push: Add parameters 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: Return a new array, which is composed 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,6,7]
splice(start,deleteCount,val1,val2,...): Delete deleteCount items from the start position and insert val1, val2,...
var a = [1,2,3,4,5]; var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4] var b = a.splice(0,1); //同shift a.splice(0,0,-2,-1); var b = a.length; //同unshift var b = a.splice(a.length-1,1); //同pop a.splice(a.length,0,6,7); var b = a.length; //同push
reverse: Reverse 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): Sort the array according to the 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 composed of items from the specified start index to the end index in the original array
var a = [1,2,3,4,5]; var b = a.slice(2,5); //a:[1,2,3,4,5] b:[3,4,5]
join(separator): The elements of the array form a string, with separator as the separator. If omitted, the default comma is used as the separator
var a = [1,2,3,4,5]; var b = a.join("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"
The array is an internal object provided by JavaScript. It is an For a standard collection, we can add (push), delete (shift) elements, and we can also traverse the elements through a for loop. So besides arrays, can we have other collections in JavaScript?
Due to the language characteristics of JavaScript, we can dynamically add and delete properties to general objects. So Object can also be regarded as a special collection of JS. Let’s compare the characteristics of Array and Object:
//Array: /*新建:*/var ary = new Array(); 或 var ary = []; /*增加:*/ary.push(value); /*删除:*/delete ary[n]; /*遍历:*/for ( var i=0 ; i < ary.length ; ++i ) ary[i]; //Object: /*新建:*/var obj = new Object(); 或 var obj = {}; /*增加:*/obj[key] = value; (key为string) /*删除:*/delete obj[key]; /*遍历:*/for ( var key in obj ) obj[key];
From the above comparison, we can see that Object can be used as a collection. When using the Popup window to create unlimited web pages In Menu (3), I introduced the __MenuCache__ implemented by Eric, which is also a simulated collection object.
If we want to retrieve a specified value in Array, we need to traverse the entire array:
var keyword = ; for ( var i=0 ; i < ary.length ; ++i ) { if ( ary[i] == keyword ) { // todo } }
And we are To retrieve an entry with a specified key in Object, just use:
var key = ''; var value = obj[key]; // todo
Object的这个特性可以用来高效的检索Unique的字符串集合,遍历Array的时间复杂度是O(n),而遍历Object的时间复杂度是O(1)。虽然对于10000次集合的for检索代价也就几十ms,可是如果是1000*1000次检索或更多,使用Object的优势一下就体现出来了。在此之前我做了一个mapping,把100个Unique的字符mapping到1000个字符串数组上,耗时25-30s!后来把for遍历改成了Object模拟的集合的成员引用,同样的数据量mapping,耗时仅1.7-2s!!!
对于集合的遍历效率(从高到低):var value = obj[key]; > for ( ; ; ) > for ( in )。效率最差的就是for( in )了,如果集合过大,尽量不要使用for ( in )遍历。
相关推荐:
The above is the detailed content of js array operation example analysis. For more information, please follow other related articles on the PHP Chinese website!