当前位置:网站首页>JS common array methods
JS common array methods
2022-06-13 04:09:00 【Day t】
1.sort() Sorting method :
arr.sort()// Press default ascii Sort code
// From small to large
candidates.sort((a, b) => a - b)
// From big to small
candidates.sort((a, b) => b - a )
2.push() This method is to add a new element after the array , This method changes the length of the array :
const arr=[];
const ans=[];
ans.push([...arr])
console.log(ans);//[[1,2,3],[4,5,6]]
3.pop() This method deletes the last element after the array , And returns an array , This method changes the length of the array :
let arr = [1, 2, 3, 4, 5]
arr.pop()
console.log(arr)//[1, 2, 3, 4]
console.log(arr.length)//4
4.shift() Delete before shift() Used to delete and return the first element , Delete the first element , Returns the deleted element , Change the original array
var a = [1,2,3]
var b = a.shift()
console.log(a) // [2,3]
console.log(b) // 1
5.unshift() It's increasing unshift() You can add one or more elements to the front of the array , And return the new length header to add , Return length , Change the original array
var a = [2,3,4]
var b = a.unshift(0,1)
console.log(a) // [0,1,2,3,4]
console.log(b) // 5
6.Array.toString() This method converts the array to a string :
let arr = [1, 2, 3, 4, 5];
let str = arr.toString()
console.log(str)// 1,2,3,4,5
7.Array.splice( Starting position , Number of deletions , Elements ) Omnipotent method , You can add, delete and modify : Change the array itself
let arr = [1, 2, 3, 4, 5];
let arr1 = arr.splice(2, 0'haha')
let arr2 = arr.splice(2, 3)
let arr1 = arr.splice(2, 1'haha')
console.log(arr1)//[1, 2, 'haha', 3, 4, 5] Add an element
console.log(arr2)//[1, 2] Delete three elements
console.log(arr3)//[1, 2, 'haha', 4, 5] Replace an element
8.slice() shear slice(startIndex,endIndex) Return from startIndex Start ( Include ), To endIndex( barring ) The new array is returned from the array composed of the original attributes between , Do not change the original array
var a = [1,2,3]
var b = a.slice(0,1)
// If no parameter is filled in, the entire array will be cut
var c = a.slice()
console.log(a) // [1,2,3]
console.log(b) // [1]
console.log(c) // [1,2,3]
console.log(a===c) // false // Be careful a !== c
// A negative number means to count from the back to the front
var d = a.slice(-1,-2)
console.log(d) // [] Intercept from left to right , So it's []
var e = a.slice(-1)
console.log(e) // [3]
9.concat() Splicing concat() Method is used to merge two or more arrays , Return a new array , It doesn't change the original array
var a = [1,2,3]
var b = [4,5]
var c = a.concat(b)
console.log(a) // [1,2,3]
console.log(b) // [4,5]
console.log(c) // [1,2,3,4,5]
10.join();join() Method is used to convert an array to a string without changing the original array , Return the converted String
var a = [1,2,3,4,5]
console.log(a.join(',')) // 1,2,3,4,5
console.log(a) // [1,2,3,4,5]
11.reverse() Reverse the order ;reverse() Method to reverse the order of the elements in an array . It returns the inverted array , It's going to change the array .
var a = [1,3,2,7,6]
console.log(a.reverse()) // [6,7,2,3,1]
console.log(a) // [6,7,2,3,1]
12.indexOf() and lastIndexOf();indexOf( A particular element ,startIndex) from startIndex Start , Find the position of an element in the array , If exist , Returns the subscript of the first position , Otherwise return to -1
var a = [1,2,4,3,4,5]
console.log(a.indexOf(4)) // 2
console.log(a.indexOf(4,3)) // 4
13.filter() Filter filter() Method returns a new array of qualified elements in the array , The original array remains the same
filter() The argument to is a method
var a = [1,2,3,4,11]
// The first parameter is a method , There are three parameters ,current: Current value index: Current value subscript array: This array object
var b = a.filter(function(current,index,array){
return current < 10
})
console.log(b) // [1,2,3,4]
console.log(a) // [1,2,3,4,11]
14.map() Format array
map() Method to format the original array as required , Returns the formatted array . The original array remains the same
var a = [1,2,3,4,5]
// The parameters are the same as filter Method
var b = a.map(function(current,index,array){
return current + 1
})
console.log(b) // [2,3,4,5,6]
console.log(a) // [1,2,3,4,5]
15.every() Run the given function for each item of the array , If each item returns ture, Then return to true
var a = [1,2,3,4,5]
var b = a.every(function(current,index,array){
return current < 6
})
var c = a.every(function(current,index,array){
return current < 3
})
console.log(b) // true
console.log(c) // false
16.some() Run the given function for each item of the array , If one or more items exist, return ture, Then return to true
var a = [1,2,3,4,5]
var b = a.some(function(current,index,array){
return current > 4
})
var c = a.some(function(current,index,array){
return current > 5
})
console.log(b) // true
console.log(c) // false
17.forEach() Array traversal Traverse the entire array , You can't interrupt
var arr = ['a','b','c']
var copy = []
arr.forEach(function(item){
copy.push(item)
})
console.log(copy)
ES6 The new method
1. find() Find the element in the array that meets the condition for the first time , And back to , If not, return undefined. Do not change the original array .
and filter() The difference is :filter The return value is an array of all the elements that meet the conditions ,
Generally, when you need to use the found elements , use find() Method
var a = [1,2,3,4]
// b Below you need to use , Generally use find
var b = a.find(function(ele,index,array){
return ele == 1
})
var c = 3
var d = b + c
console.log(a) // [1,2,3,4]
console.log(b) // 1
console.log(d) // 4
// If you just need to judge whether the element exists
// If it's a simple array ( Non object array ), It is generally used Array.includes(value) Method
// If it is an object array , You can use Array.some() Method
var a = [1,2,3]
console.log(a.includes(1)) // true
var a = [{
"name": "xiaoming" },{
"name": "xiaohong"}]
console.log(a.some(function(ele){
return ele.name == 'xiaoming'
})) // true
2.findIndex() Method
findIndex() The effect is the same as indexOf(), Returns the first subscript that satisfies the condition , And stop looking .
The difference is that findIndex() The parameter of is a callback function , And is generally used for object arrays
var a = [1,2,3,4]
var b = a.findIndex(function(ele,index,array){
return ele === 2
})
var c = a.indexOf(2)
console.log(a) // [1,2,3,4]
console.log(b) // 1
console.log(c) // 1
3.includes()
includes() Method , Returns a Boolean value . The parameter is one value, Generally used for simple arrays .
For complex arrays , You can use some() Method substitution includes() Method
var a = [1,2,3]
console.log(a.includes(1)) // true
4.Array.isArray() Method Used to determine whether an element is an array
Array.isArray([]) // true
Array.isArray({
}) // false
边栏推荐
- [test development] automated test selenium (II) -- common APIs for webdriver
- 单片机:D/A 输出
- Solution to failure to download files by wechat scanning QR code
- ROS中的msg消息
- Single chip microcomputer: main index of a/d (analog-to-digital conversion)
- USB-IF BC1.2充电协议解读
- 建模杂谈系列143 数据处理、分析与决策系统开发的梳理
- Common array methods in JS (map, filter, foreach, reduce)
- 干预分析 + 伪回归
- 重读经典:《End-to-End Object Detection with Transformers》
猜你喜欢
环评图件制作-数据处理+图件制作
Lightweight digital mall system based on PHP
Goframe day 4
高等数学(第七版)同济大学 习题1-3 个人解答
干预分析 + 伪回归
Goframe day 5
Single chip microcomputer: a/d differential input signal
EIA map making - data processing + map making
Use the visual studio code terminal to execute the command, and the prompt "because running scripts is prohibited on this system" will give an error
微信扫描二维码无法下载文件的解决办法
随机推荐
Real time requirements for 5g China Unicom repeater network management protocol
dumi 搭建文檔型博客
R: Airline customer value analysis practice
建模杂谈系列143 数据处理、分析与决策系统开发的梳理
MCU: RS485 communication and Modbus Protocol
leetcode. 1 --- sum of two numbers
[notes] summarize common horizontal and vertical centering methods
Redis-HyperLogLog-基数统计算法
Manage PC startup items
Single chip microcomputer: pcf8591 application program
MCU: NEC protocol infrared remote controller
[kubernetes series] pod chapter actual operation
Interpretation of mobile phone private charging protocol
PAT 1054 The Dominant Color
Summary of meeting between president Ren and scientists and experts in system engineering
Lightweight digital mall system based on PHP
try-catch finally执行顺序的例题
Dumi construit un blog documentaire
Discussion sur la modélisation de la série 143
高等数学(第七版)同济大学 习题1-3 个人解答