A brief summary of commonly used array operation methods for js
Updated: June 20, 2014 09:22:19 Submission: junjie
This article mainly introduces a brief summary of commonly used array operation methods for js. It directly uploads the code. There are detailed comments in the code. Friends who need it can refer to it.
//Array segmentation in javascriptvar colors = ["red","green","blue"]; //alert(()); alert(("|")); //The return result is red|green|bluevar colors = ["red","green","blue",null]; alert(("|"));//red|green|blue| //Note that when there is a value in the array that is null or undefined, the returned result is represented by an empty string------------------------------------------- //Delete and add arrayvar colors = ["red","green","blue"]; //alert(()); ("white","test");//The result returned is the length of the arrayalert(("|"));//The result is red|green|blue|white|test//Add elements to the beginning of the arrayvar colors = ["red","green","blue","test"]; var item = ("first");//Add an element at the beginning of the arrayalert(("|"));//Return the last array //Delete the elementvar colors = ["red","green","blue","test"]; var item = ();//Return the deleted option result testalert(("|"));//Return the last array result red|green|blue//Delete the beginning elementvar colors = ["red","green","blue","test"]; var item = ();//The first option to delete the arrayalert(("|"));//Return the last array------------------------------------------------- //Array order example//The order is reversedvar colors = ["red","green","blue","test"]; (); alert(colors);//The result is: test, blue, green, red//Array sortvar values = [0,1,5,10,7]; (compare); alert(values); //(values); } function compare(value1,value2){ if(value1<value2){ return 1 ; }else if(value1>value2){ return -1 ; }else return 0 ; } ----------------------------------------------------- //Add array to array concat() methodvar colors = ["color","red"]; var colors2 = (["ccc","bbbb"],'3333',['vvccxx',['oolll','lll']]); alert(colors2);//The return result is: color, red, ccc, bbbb,3333, vvccxx, oolll, lll //Slice() method copying elements in the array will not destroy the previous elementsvar colors = ["color","red",'eeee','221111']; var colors2 = (1);//Copy from 1alert(colors2);//The result is: red,eeee,2211111 var colors = ["color","red",'eeee','221111']; var colors2 = (1,3);//Copy from 1 to end at 3rd positionalert(colors2);//The result is red,eeee--------------------------------------------------------------------- //Delete elements in the arrayvar a = [1,2,3,5,8]; var r = (0,2); //Delete the first 2 itemsalert(a);//The result is 3,5,8 var a = [1,2,3,5,8]; var r = (1,1,100,200); //Delete an item from the second number and insert 100 200alert(a);//turn out1,100,200,3,5,8
Related Articles
js' toLowerCase method usage example
This article mainly introduces the usage of the toLowerCase method of js, and analyzes the techniques of using the toLowerCase method for converting lowercase. Friends who need it can refer to it2015-01-01Detailed explanation of JS implementation calculator and example code (I)
This article mainly introduces the detailed explanation of JS implementation calculator and related information about the example code. Here we explain the ideas and implementation steps of implementing the calculator one by one. Friends who need it can refer to it.2017-01-01JavaScript entry events, cookies, timing, etc.
This article starts from the end of the previous article and explains some other basic JavaScript language concepts, continuing to provide beginners with a basic understanding of the language.2011-10-10JavaScript Learning Notes (Twenty) Obtain and set the properties of elements (properties)
Introduction to the characteristics (attributes) of obtaining and setting elements of JavaScript learning notes. Friends who study js can refer to it.2012-06-06Introduction to the differences between JS special functions (Function() constructor, function direct quantity)
Functions are declared in this way: keyword function, function name, a set of parameters, and code to be executed in parentheses; Function() constructor is actually a complete object2013-05-05Detailed interpretation of the use of promises in JavaScript programming
This article mainly introduces a detailed interpretation of the use of Promise in JavaScript programming. It is the basic knowledge in the introduction to JS. Friends who need it can refer to it.2015-07-07Detailed explanation of WeChat and QQ time format template examples
This article mainly introduces the relevant information about detailed explanation of WeChat and QQ time format template examples. Friends who need it can refer to it.2016-10-10Several different traits of TypeScript
This article mainly introduces several different characteristics of TypeScript. First, let’s take a look at what TypeScript is? TypeScript is an open source language launched by Microsoft for the development of large JS applications. The author is Anders Hejlsberg, the famous father of C#.2015-04-04JavaScript Advanced Programming (3rd Edition) Learning Notes 9 js functions (Part 2)
A function is an object that has all the characteristics of a general object. In addition to the function having its own properties and methods, it can also be used as a reference type value. In fact, in the previous example, we have used a function as the value of an object's attribute, and for example, a function can also be used as a parameter or return value of another function. A callback function in asynchronous processing is a typical usage.2012-10-10Deeply understand the principles of JavaScript's React framework
This article mainly introduces the principles of JavaScript's React framework, including its comparison with AngularJS. Friends who need it can refer to it2015-07-07