


js object JS introduction to Array object operation summary_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 the subscript of the array starts from zero, the subscript of the created element will be from zero to size -1.
element0,...,elementN This will create 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 the constructor of Array 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 argument.
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:
function ConcatArrayDemo(){
var a , b, c, d;
a = new Array(1,2,3);
b = "JScript";
c = new Array(42, "VBScript);
d = a.concat(b, c);
// Return array [1, 2, 3, "JScript", 42, "VBScript"]
return(d);
}
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:
function JoinDemo(){
var a, b;
a = new Array(0, 1,2,3,4);
b = a.join("-");
//Return: "0-1-2-3-4"
return(b);
}
pop method: Removes the last element in the array and returns the element if the array is empty.
. 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 as a single element to the array if you want to merge two. or elements in multiple arrays, please use the concat method.
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 not contiguous, the reverse method will create elements in the array to fill the gaps in the array. The value of all elements created in this way is undefined.
function ReverseDemo(){
var a, l // 변수를 선언합니다.
a = new Array(0,1,2,3,4); // 배열을 만들고 값을 할당합니다.
l = a.reverse(); // 배열의 내용을 반대로 바꿉니다.
//Return: l=[4,3,2,1,0]
return(l) // 결과 배열을 반환합니다.
}
shift 메소드 는 배열의 첫 번째 요소를 제거하고 해당 요소를 반환합니다.
slice 메소드 는 arrayObj의 지정된 부분을 포함하는 Array 객체를 반환합니다. slice 메소드는 end에 지정된 요소까지 복사하지만 포함하지는 않습니다. start 가 음수이면 length start로 처리합니다. 여기서 length는 배열의 길이입니다. end가 음수이면 length end로 처리합니다. 여기서 length는 배열의 길이입니다. end를 생략하면 slice 메서드가 arrayObj의 끝에 복사됩니다. start 앞에 end가 나타나면 요소가 새 배열에 복사되지 않습니다. 예:
//마지막 요소 제외 , myArray의 모든 요소는 newArray에 복사됩니다.
newArray = myArray.slice(0, -1)
정렬 방법은 Array 객체를 적절하게 정렬하지 않습니다. 새 Array 객체는 다음과 같습니다. 생성되었습니다. sortfunction 인수에 함수가 제공되는 경우 함수는 다음 값 중 하나를 반환해야 합니다.
전달된 첫 번째 인수가 두 번째 인수보다 작은 경우 음수 값입니다.
두 인수가 모두 같으면 0입니다.
첫 번째 매개변수가 두 번째 매개변수보다 큰 경우 양수 값입니다.
function SortDemo(){
var a, l ; // 변수를 선언합니다.
a = new Array("X" ,"y" ,"d", "Z", "v","m","r")
l = a.sort(); 배열을 정렬합니다.
return(l); // 정렬된 배열을 반환합니다.
}
splice 메소드 는 start 위치부터 지정된 수의 요소를 제거하고 새 요소를 삽입할 수 있습니다. 따라서 arrayObj를 수정합니다. 반환 값은 제거된 요소로 구성된 새로운 배열 객체입니다. 형식은 다음과 같습니다.
arrayObj.splice (start, deleteCount, [ item1[, item2[, . . . . . . [,itemN]]]])
toLocaleString 메소드 에 대해서는 다음에서 설명합니다. Date 개체, 일반적으로 이 메서드는 사용자에게 반환될 뿐이며 코드에서 계산되지 않습니다.
unshift 메소드 는 이러한 요소를 배열의 시작 부분에 삽입하므로 전달된 순서대로 배열에 표시됩니다. 형식은 다음과 같습니다.
arrayObj.unshift ([item1[, item2 [, . . . [, itemN]]]])
valueOf method&toString() 배열의 요소는 문자열로 변환됩니다. 쉼표로 구분되어 함께 연결됩니다. 동작은 Array.toString 및 Array.join 메소드와 동일합니다.
이제 Array 객체에 대한 모든 것이 거의 완성되었습니다. 스크립팅 지식을 통합하기 위해 정리한 예제가 도움이 된다면 많은 부분을 참고하시기 바랍니다. 불편을 드려 죄송합니다!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.
