Introduction to JS Array object analysis_javascript skills
However, in the process of learning the script prototype.js, I found that there are some methods that we rarely use, but some methods seem to be very classic. The script library is constantly increasing, and everything in front of us... Let's continue our introduction to JS and learn about Array arrays today.
First let’s take a look at its definition:
var arrayObj = new Array()
var arrayObj = new Array([size])
var arrayObj = new Array([element0[, element1[, [, elementN]]]])
Where: arrayObj is the variable name assigned to the Array object.
size Since array subscripts start from zero, the subscripts of the created elements will be from zero to size -1.
element0,...,elementN This creates an array of length n 1 with n 1 elements. More than one element is required to use this syntax.
If only one argument is passed to Array's constructor, and that argument is a number, it must be an unsigned 32-bit integer (approximately 4 billion). This value becomes the size of the array. If the value is numeric but less than 0 or not an integer, a runtime error occurs.
If the value passed to the Array constructor is a single value and not a numeric value, the length property is set to 1 and the unique element value becomes the single passed-in parameter.
Since JS arrays are parsed arrays, that is, although multiple elements can be assigned to an array, only the elements containing data actually exist. This reduces the amount of memory used by the array.
The Array object has three built-in properties: constructor, length, prototype. I will write a special article about constructor, prototype, arguments and other object properties, so I won’t go into details here. Below we mainly look at some of the built-in methods of Array. This is very important to us because we will use this stuff frequently.
concat method: concatenates two or more arrays and returns a new array. It is worth noting that (reference type) the object parameters copied from the array being connected to the new array still point to the same object after copying. No matter which one of the new array and the source array is changed, the other will be caused. Change; (value type) For the numerical value or string connected to the new array, only its value is copied. Changes in the value in one array do not affect the value in the other array. Example:
1 function ConcatArrayDemo(){
2 var a, b, c, d;
3 a = new Array(1,2,3);
4 b = "JScript";
5 c = new Array(42, "VBScript);
6 d = a.concat(b, c);
7 // Return array [1, 2, 3, "JScript", 42, "VBScript"]
8 return(d);
9 }
10
join method: Convert the items in the array into a string according to a specific delimiter and return it. The default delimiter is comma, example:
1 function JoinDemo(){
2 var a, b;
3 a = new Array(0,1,2,3,4);
4 b = a.join("-");
5 //Return: "0-1-2-3 -4"
6 return(b);
7 }
pop method: removes the last element in the array and returns the element. Returns if the array is empty undefined.
push method: will add the new elements in the order they appear, if one of the parameters is an array, then the array will be added to the array as a single element if you want to merge elements from two or more arrays. , please use the concat method. The
reverse method reverses the position of elements in an Array object. During execution, this method does not create a new Array object. If the array is discontinuous, the reverse method will. Create elements in an array to fill the gaps in the array. The values of all the elements created are undefined. The code is as follows:
The shift method removes the first element in an array and returns that element.
The slice method returns an Array object, which contains the specified part of arrayObj. The slice method copies up to, but not including, the element specified by end. If start is negative, treat it as length start, where length is the length of the array. If end is negative, it is treated as length end, where length is the length of the array. If end is omitted, the slice method copies until the end of arrayObj. If end appears before start, no elements are copied to the new array. Example:
//Except for the last element, all elements in myArray are copied to newArray:
newArray = myArray.slice(0, -1)
The sort method sorts the Array object appropriately Sorting; no new Array objects are created during execution. If a function is provided for the sortfunction argument, the function must return one of the following values:
· Negative value if the first argument passed is smaller than the second argument.
· Zero if both parameters are equal.
· Positive value if the first parameter is larger than the second parameter.
1 function SortDemo(){
2 var a , l; // Declare variables.
3 a = new Array("X" ,"y" ,"d", "Z", "v","m","r");
4 l = a.sort(); // Sort the array.
5 return(l); // Return the sorted array.
6 }
The splice method can modify arrayObj by removing a specified number of elements starting from the start position and inserting new elements. The return value is a new Array object consisting of the removed elements. The format is as follows:
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
toLocaleString method will be explained in the Date object. Generally, this method just returns to User, not calculated as in code. The
unshift method inserts the elements at the beginning of an array, so they appear in the array in the order in which the arguments are sequenced. The format is as follows:
arrayObj.unshift([item1[, item2 [, . . . [, itemN]]]])
valueOf method&toString() The elements of the array are converted to strings, which are separated by commas Separate, connect together. Its operation is the same as the Array.toString and Array.join methods.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can
