Detailed explanation of JS array usage
This article mainly shares with you the detailed explanation of JS array usage, mainly in the form of code. I hope it can help everyone.
1. Adding and deleting arrays The push() method adds one or more elements to the end of the array
a = []; a.push("zero") // a = ["zero"] a.push("one","two") // a = ["zero","one","two"];
The method to delete an element at the end of the array is the pop() method. The principle is to reduce the length of the array by 1 and return the deleted element.
2. join()
Array.join()方法将数组中的所有的元素转化为字符串并连接一起,返回最后生成的字符串。默认是是逗号,中间可以是任意的字符。
var bb = ['abc','cd',1,5]; bb.join("/") //"abc/cd/1/5"
The Array.join() method is the reverse operation of the String.split() method, which is Split string into array.
var str = "abc/cd/1/5"; str.split("/") //["abc", "cd", "1", "5"]
3、reverse()
Array.reverse()将数组中的元素顺序颠倒,
var s = [1,2,3]; s.reverse().join("-") //"3-2-1"
4、sort()
对数组中的元素进行排序,返回排序后的数组。当sort()不带参数时,是按字母表排序。
var a = new Array("banaa","apple","cherry"); a.sort(); var s = a.join("/"); //"apple/banana/cherry"
进行数组排序,要传递一个比较函数,假设第一个参数在前,比较函数返回一个小于0的数值,
var a = [33,4,111,222]; a.sort() //111,222,33,4 a.sort(function(a,b){return a-b}); //4,33,222,111
5 , concat()
Array.concat()方法创建并返回一个新数组,连接的数组元素,不是数组本身,concat()不会修改调用的数组
var a = [1,2,3];var b = a.concat(); 数组的复制//b = [1,2,3]a.concat([4,5]); //[1,2,3,4,5]
6, slice(). The Array.slice() method returns a fragment or subarray of the specified array. The parameters are the starting position and the ending position
var a = [1,2,3,4,5]; var b = a.slice(0,3) //[1,2,3] a.slice(3) //[4,5] a.slice(1,-1) //[2,3,4] a.slice(-3,-2) //[3]
7, splice()
Array.splice()方法在数组中插入或删除元素,不同于slice(),concat(),会修改数组。
var a = [1,2,3,4,5,6,7,8]; var b = a.splice(4); //a = [1,2,3,4],b=[5,6,7,8] var c = a.slice(1,2) //a = [1,4] b=[2,3] var a = [1,2,3,4,5]; a.splice(2,0,'a','b') //a = [1,2,'a','b',3,4,5]
8、push()、pop()
push()在数组的尾部添加一个或者多个元素,并返回数组的新的长度。pop()删除最后一个元素,返回删除的元素。
var stack =[]; stack.push(1,2) //返回2 stack.pop() //返回2
9、unshift()、shif()
在数组的头部进行操作,unshift()在头部添加一个或多个元素,返回长度,shift()删除数组的第一个元素,并返回
var a = []; a.unshift(1,2,3,4) //a:[1,2,3,4] 返回4 a.shift() //a:[2,3,4] 返回1
es5 Array methods:
遍历、映射、过滤、检测、简化、搜索数组
1, forEach()
是从头至尾遍历数组,为每个元素调用制指定的函数,该函数接收三个参数,数组元素(value)、索引(index)、数组本身(arr);
var data = [1,2,3,4,5]; //每个元素值自加1 data.forEach(function(v,i,a){ a[i] = v + 1; }) //[2,3,4,5,6]
2, map()
map()方法将调用的数组的每一个元素传递给指定的函数,返回一个新数组
a = [1,2,3]; b = a.map(function(x){ return x*x; }) //[1,4,9]
3, filter()
filter()方法是对数组的每一个元素的,在传递函数中进行逻辑判断,该函数返回true、false
var a = [1,2,3,4,5]; var b = a.filter(function(x){return x < 3}) //[1,2]
4, every() and some()
every()是对所有的元素在传递函数上进行判断为true,some()是对其中的一个进行判断。
var a = [1,2,3,4,5]; a.every(function(x){ return x%2 === 0 }) //false,不是所有的值都是偶数 a.some(function(x){ return x%2 === 0; }) //true,a含有偶数
5, reduce() and reduceRight ()
将数组元素进行组合,生成单个值
var a = [1,2,3,4,5]; var sum = a.reduce(function(x,y){return x+y},0) //数组求和 var product = a.reduce(function(x,y){return x*y},1) //数组求积 var max = a.reduce(function(x,y){return (x>y)?x:y}) //求最大值 reduce()函数需要两个函数,第一个是执行简化操作的函数,第二个是初始值。
6, indexOf() and lastIndexOf()
搜索整个数组中给定的值的元素,返回找到的第一个元素的索引值,没有找到返回-1,
var a = [0,1,2,1,0]; a.indexOf(1) //1 a.lastIndexOf(1) //3 a.indexOf(3) //-1
es6数组方法:
1、Array.of()方法,创建一个包含所有参数的数组
let items = Array.of(1,2);//[1,2] let items = Array.of(2) //[2] let items = Array.of("2")//["2"]
2、Array.from(),将非数组对象转换为正式数组3、find()和findIndex()接收两个参数,一个是回调函数,另一个是可选参数,find()返回查找到的值,findeIndex()返回查找到的索引值,
let number = [25,30,35,40,45]console.log(number.find(n => n > 33)) //35console.log(number.findIndex(n => n >33)) //2
数组去重
1、遍历数组去重
function unique(obj){ var arr = []; var len = obj.length; for(var i=0;i<len;i++){ if(arr.indexOf(obj[i]) == -1){ arr.push(obj[i]) } } return arr;}unique([1,1,1,2,3])[1,2,3]
2、对象键值对法
function unique(obj){ var tar = {},arr = [],len = obj.length,val,type; for(var i = 0;i<len;i++){ if(!tar[obj[i]]){ tar[obj[i]] = 1; arr.push(obj[i]) } } return arr;}
3、es6 new Set()方法
Array.from(new Set([1,2,3,3,3])) //[1,2,3]
The above is the detailed content of Detailed explanation of JS array usage. For more information, please follow other related articles on the PHP Chinese website!

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



Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

WPS is a commonly used office software suite, and the WPS table function is widely used for data processing and calculations. In the WPS table, there is a very useful function, the DATEDIF function, which is used to calculate the time difference between two dates. The DATEDIF function is the abbreviation of the English word DateDifference. Its syntax is as follows: DATEDIF(start_date,end_date,unit) where start_date represents the starting date.

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

The ISNULL() function in MySQL is a function used to determine whether a specified expression or column is NULL. It returns a Boolean value, 1 if the expression is NULL, 0 otherwise. The ISNULL() function can be used in the SELECT statement or for conditional judgment in the WHERE clause. 1. The basic syntax of the ISNULL() function: ISNULL(expression) where expression is the expression to determine whether it is NULL or

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a
