Detailed explanation of steps to use JS array method
This time I will bring you a detailed explanation of the steps to use the JS array method. What are the precautions when using the JS array method? The following is a practical case, let’s take a look.
1.join
Function: Splice the array into a string by specifying characters .
Syntax: string arr.join([separator = ',']);
Parameters: separator can If omitted, defaults to a comma . If seprator is an empty string , then all elements in the array will be concatenated directly.
Return value: The concatenated string.
<script> var arr=[2,3,4,5]; console.log(arr.join());//"2,3,4,5" console.log(arr.join(""));//"2345" console.log(arr.join("+"));//"2+3+4+5" console.log(arr);//[2,3,4,5] </script>2.push
Function: Add one or more elements to the end of the array.
Syntax: number arr.push(element1, ..., elementN);
Parameters: elementN, one or more elements.
Return value: The length of the new array.
Note: will modify the original array.
<script> var att=[1,2,3,4]; console.log(att.push("a","b"));//6 console.log(att);//[1,2,3,4,"a","b"]; </script>3.unshift
Function: Add one or more elements to the head of the array.
Syntax: number arr.push(element1, ..., elementN);
Parameters: elementN, one or more elements.
Return value: The length of the new array.
Note: will modify the original array.
<script> var att=[1,2,3,4]; console.log(att.unshift("a","b"));//6 console.log(att);//["a","b",1,2,3,4]; </script>4.pop
Function: DeletearrayLast a element.
Syntax: mixed arr.pop();
Return value: The deleted element.
Note: will modify the original array.
<script> var arr=[1,2,3,4]; console.log(arr.pop());//4 console.log(arr);//[1,2,3] </script>5.shift
Function: Delete the first element of the array. Syntax:
mixed arr.pop();Return value:
The deleted element. Note:
will modify the original array. <script>
var arr=[1,2,3,4];
console.log(arr.shift());//1
console.log(arr);//[2,3,4]
</script>
6.concat
Merge the incoming array or non-array value with the original array, form a new array and return . Syntax:
array array.concat(value1, value2, ..., valueN); Parameters:
valueN refers to the array or non-array value that needs to be merged with the original array.Return value:
Merged arrayNote:
will not modify the contents of the original array. <script>
var att=[1,2,3];
var attO=["a","b"];
var attT=[5,true];
console.log(att.concat(10,attO,attT));//[1,2,3,10,"a","b",5,tru]
console.log(att);//[1,2,3]
</script>
7.reverse
Reverse the position of the elements in the array.Syntax: array arr.reverse( )
Return value:
The array after reversing the orderNote:
willchange the original array.
<script> var arr = [1,2,3,6,5,4]; var res = arr.reverse(); res.push( "a" ); console.log( res );//[4,5,6,3,2,1,"a"] console.log( arr );//[4,5,6,3,2,1,"a"] </script>8.slice
Function:
Intercept a part of the **array and return this new array.Syntax:
array arr.slice([begin[,end]]);Return value:
The new array of after interceptionNote:
Contains the starting position, does not include includes the ending position, will not change the original array. If nothing is passed in, the original array will be array. <script>
var arr = ["a","b","c","d"];
// var res = arr.slice( 1,3 );//["b","c"]
// var res = arr.slice( 3,1 );//[]
var res = arr.slice( -3,-1 );//["b","c"]
var res = arr.slice();//["a","b","c","d"]
console.log( res );
console.log( arr );//["a","b","c","d"]
</script>
9.splice
Function:
Replace old elements with new elements to modify the contents of the arraySyntax:
array array.splice(start, deleteCount[, item1[, item2[, ...]]])Parameters:
- start 起始位置 - deleteCount 删除长度 - item 添加的内容Return value: An
array composed of the deleted elements Note : 作用: 对数组的元素进行排序。 相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章! 推荐阅读:
modifies the contents of the original array. <script>
var att="Liangzhifang".split("");
console.log(att.splice(2,4,["a"]));//["a","n","g","z"]
console.log(att);//["L","i",["a"],"h","i","f","a","n","g"]
</script>
10.sort
语法: array arr.sort([compareFunction]);
参数: compareFunction可选。用来指定按某种顺序进行排列的函数。如果省略,元素按照转换为的字符串的诸个字符的Unicode位点进行排序。
返回值: 排序后的数组.<script>
//当什么都不传入的时候,sort()默认由小到大排列。
var attr=[9,5,4,3,2];
console.log(attr.sort());//[2, 3, 4, 5, 9]
//传入function的时候,sort()内的function返回值大于0,由小到大排列。
var attrO=[9,5,4,3,2];
console.log(attrO.sort(function () {
return 1;
}));//[2, 3, 4, 5, 9]
//传入function的时候,sort()内的function返回值小于或者等于0,数组序列不变。
var attrT=[9,5,4,3,2];
console.log(attrT.sort(function () {
return -1;
}));// [9, 5, 4, 3, 2]
//由于sort内部是随机抽取两个值,我们在利用function函数的返回值,大于0的时候,交换位置。小于或者等于0的时候不变,来排序。
//以下是由小到大排序
var attrTh=[9,5,4,3,2];
console.log(attrTh.sort(function (a,b) {
return a-b;
}));// [2, 3, 4, 5, 9]
//以下是由大到小排序
var attrF=[9,5,4,3,2];
console.log(attrF.sort(function (a,b) {
return b-a;// [9, 5, 4, 3, 2]
}));
</script>
The above is the detailed content of Detailed explanation of steps to use JS array method. For more information, please follow other related articles on the PHP Chinese website!

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

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.

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.

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.

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.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
