当前位置:网站首页>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
边栏推荐
- 单片机:I2C通信协议讲解
- Lambda end operation find and match allmatch
- Principle, composition and functions of sensors of Dajiang UAV flight control system
- Lambda termination operation find and match anymatch
- El expression
- Introduction and use of ES6
- leetcode.1 --- 两数之和
- Lambda end operation find and match findany
- On the value of line height
- Interpretation of usb-if bc1.2 charging protocol
猜你喜欢

Real time question answering of single chip microcomputer / embedded system
![[test development] installation of test management tool Zen path](/img/8b/363e393bdb2d3400a2e8361af8677d.png)
[test development] installation of test management tool Zen path

2022春学期总结

R: Airline customer value analysis practice

USB-IF BC1.2充电协议解读

手机私有充电协议解读

No more! Another member of the team left..

Single chip microcomputer: a/d differential input signal

Alipay native components (hotel time selection)

5G China unicom AP:B SMS ASCII 转码要求
随机推荐
Manage PC startup items
Call C function in Lua
Summary of meeting between president Ren and scientists and experts in system engineering
ET框架-22 创建ServerInfo实体及事件
Koa file upload and download
10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects
Promise combined with await
单片机/嵌入式的实时性疑问解答
dumi 搭建文檔型博客
Differences and relations between three-tier architecture and MVC
MCU: NEC protocol infrared remote controller
[test development] file compression project practice
【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
机器人避障系统基础
Redis hyperloglog cardinality statistics algorithm
单片机:EEPROM介绍与操作
大疆无人机飞控系统的原理、组成及各传感器的作用
基于DE2-115平台的VGA显示
dumi 搭建文档型博客
任总与系统工程领域科学家、专家会谈纪要