Detailed explanation of basic methods of arrays in js
This article mainly shares with you a detailed explanation of the basic methods of arrays in js. I hope it can help you.
1. arr.join( )
//join 数组==>字符串,该字符串以特定字符分隔开. var arr = ['A','B','C']; var str = arr.join('|'); console.log(str); console.log(typeof str); console.log(arr); console.log(typeof arr);//object
2. arr.push( ) arr.pop( )
var arr = ['A','B','C']; //push添加元素,在数组之后添加元素,返回的是新数组的长度 var count = arr.push('D','E'); console.log(count); console.log(arr); //pop()删除数组的最后一元素并且返回该元素,数组长度减去1 var count = arr.pop(); console.log(count); console.log(arr);
3. arr.unshift ( ) arr.shift( )
var arr = ['A','B','C']; //shift()删除数组第一个元素,并且返回该元素 console.log(arr.shift()); console.log(arr); //unshift()添加元素,在数组之前添加,返回的是新数组的长度 console.log(arr.unshift('D','E')); console.log(arr);
4. arr.reverse( )
//reverse()倒序数组,返回的是倒序之后的数组,原数组的顺序改变 var arr = ['A','B','C']; console.log(arr.reverse()); console.log(arr);
//concat()连接数组并且返回,连接后的类型为数组 var arr = ['A','B','C']; var copyArr = arr.concat(11,'F',["123",'4345']); console.log(copyArr); console.log(arr); copyArr.push('r','556'); console.log(copyArr);
5. arr.concat( )
//concat()连接数组并且返回,连接后的类型为数组 var arr = ['A','B','C']; var copyArr = arr.concat(11,'F',["123",'4345']); console.log(copyArr); console.log(arr); copyArr.push('r','556'); console.log(copyArr);
6. indexOf( ) lastIndexOf( )
//indexOf() //一个参数返回的是数组中的下标 //第二个参数表示:从什么位置开始查找.如果找到返回的是该元素在数组中的下标,找不到则返回-1. var arr = [1,5,9,32,74,36,8]; console.log(arr.indexOf(5,1)); //lastIndexOf() //一个参数:表示需要查询的元素,返回值是该元素的下标 //两个参数:第一个表示需要查询的元素,第二个表示从什么位置开始查询; //该方法查询方向是反向,但是返回值仍按照正序排列 var arr1 = [1,5,9,32,74,36,8]; console.log(arr1.lastIndexOf(37));
Summary:
// push();向数组最后位置添加元素,返回的是数组的长度 // pop();删除数组最后一个元素,返回的是删除的该元素 // unshift();向数组的最前端添加元素,返回的是数组的长度 // shift();删除数组第一个元素,返回的是删除的该元素 //join();把数组各个元素拼接为字符串,并且以特定的字符分隔开, //返回值是带有特殊字符分隔的字符串 //reverse()倒序数组 //返回的是倒序之后的数组 //concat();拼接数组,返回拼接之后的数组 //indexOf();执行查询操作 //如果只有一个参数, 则查询该元素; 找到的话, 返回该元素的下标, 找不到的话, 返回-1. //如果含有两个参数, 则: 第一个参数表示查询的目标元素 // 第二个参数表示正序从‘参数二’开始查找 //lastIndexOf();执行查询操作 //如果只有一个参数, 则查询该元素; 找到的话, 返回该元素的下标, 找不到的话, 返回-1. //如果含有两个参数, 则: 第一个参数表示查询的目标元素 // 第二个元素表示反序从‘参数二’开始查找
Related recommendations:
Six types of JS array deduplication Method sharing
Detailed explanation of js array deduplication and deflattening
The most practical JS array function organization
The above is the detailed content of Detailed explanation of basic methods of arrays in js. 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
