当前位置:网站首页>Common methods of JS array
Common methods of JS array
2022-07-02 10:56:00 【Ape L】
One 、 Array common methods
1.Array.isArray( Judgment content ) Determine whether it is an array , Returns... For the array true Do not return for array false
2. let arr2=Array.of( Parameters ) Definition arr2 An array of parameters
3.let arr=Array.from(obj) Replace the object with an array
( Generate a new array )
4. Remove elements
arr.pop() Delete the last element of the array pop
arr.shift() Delete the first element of the array shift()
5. Additive elements
arr.push( Add content ) Add later push()
arr.unshift(1) Add unshift()
6 Inversion array arr.reverse()
7.arr.splice() Delete , Add replacement array
2 Parameters Delete
arr.splice( The starting position , Bottom position );
3 Parameters Add or replace
arr.splice( Starting position , length , Replace or add elements );
arr.splice(0,0,100); Add... At the beginning 100
arr.splice(0,1,100); Replace 0~1 That is, the first position element , Replace with 100
arr.splice(-1,0,500) Add... At the penultimate position 500 Number of numbers
arr.splice(-1,1,600) Replace the last element with 600
8.arr.sort() Scheduling problem Default ascending sort , From small to large
(1) Array elements are numbers , Sort
arr.sort((a,b)=>a-b); Ascending From small to large
arr.sort((a,b)=>b-a); Descending From big to small
(2) Array elements are strings , Call directly arr.sort() Method
(3) Array objects
9.arr.copyWithin() Copy and change the array
arr.copyWithin(index,start,end);
10.arr.fill(value,start,end) Fill change array
11. arr.slice(start,end) Copy array elements
12 arr.join() Convert an array to a string
13 arr.concat() Linked array
14 arr.toString() Array to string
15 arr.indexOf() Query whether the element exists
arr.indexOf(" Query element ", Query start position ); There is no return -1, There is a return subscript position
15 arr.lastIndexOf(value,index) Reverse search
index Start looking for the penultimate
If there are multiple , Return the last element position , Otherwise, there is no return -1
16 arr.includes(value,index) The result returns a Boolean value
Two 、 Array traversal method
1.arr.forEach() Traversing elements
arr.forEach((value,index)=>{
console.log(' The value is '+value);
console.log(' Subscript to be '+index);
});
2.arr.some() Check whether the value in the array meets the condition , Having one can
// Check whether there is greater than 888 Value . If there is a return true, Otherwise return to false
arr.some(value=>value>888);
3.arr.every() Check whether all the conditions in the array are met
let arr=[1,2,3,4];
arr.every(value=>value>0) // return true, All conditions are met
arr.every(value=>value>2) // return false, Only partially satisfied
4.arr.filter() Filter array Return a new array
5.arr.map() Treat each element , Return a new array
6.keys() Traverse the key name ,values() Traverse the key values , entries() Traversal key value pairs
arr=['a','b','c'];
for(let i of arr.keys()){
console.log(i)//keys() Ergodic subscript 0 1 2
}
for(let item of arr.values()){
console.log(item)//values() Traversal value a b c
}
for (let [i,item] of arr.entries()){
console.log(i,item)//entries() Traverse subscripts and contents
}
keys values entries
7.reduce() Accumulator of array , Merge into one value .
grammar arr.reduce((total, value, index, arr), init)
Parameters total( must ): Initial value , Then it is the return value of the previous callback .
value( must ): The value of an array element .
index( Optional ): Index value .
arr( Optional ): Array objects .
init( Optional ): Initial value .
Return value : The accumulated value
let arr = [1,2,3,4,5]
let sum = arr.reduce((total,value)=>total +value)
console.log(sum)//15
Find out how many elements are in the array
8.find() / findIndex() Find the array member by condition [ES6]
grammar arr.find(function(value, index, arr), this);
The parameter value is the same as froEach equally
Return value : find() Return the first qualified array member , If there is no return undefined. findIndex() Return the index of the qualified array members .
let arr= [1,2,3,4,5]
let result = arr.find(item=>item>3)
console.log(result)//4 The first value is
let result1 = arr.findIndex(item=>item>3)
console.log(result1)//3 The subscript of the first value
9. arr.flat() Depth traversal expansion array
Array de reordering
let arr = [1,3,2,4,8,6,[5,1,3,5,9,10,5,[1,5,3,6,7,8,11,7,66]]]
let newarr = Array.from(new Set(arr.flat(Infinity))).sort((a,b)=>a-b)
console.log(newarr)//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 66]
Array scrambling
var arr = [1,2,3,4,5,6,7,8,9]
for(let i =0;i<arr.length;i++){
let sum = parseInt(Math.random()*(arr.length-1))
let copyarr = arr[i];
arr[i] = arr[sum]
arr[sum] = copyarr
}
console.log(arr)
It's all about carrying bloggers to learn , But taking notes is more familiar
边栏推荐
- Transport Optimization abstraction
- Pywin32 opens the specified window
- MongoDB-快速上手MongoDB命令行的一些简单操作
- 618再次霸榜的秘密何在?耐克最新财报给出答案
- 【付费推广】常见问题合集,推荐榜单FAQ
- "Matching" is true love, a new attitude for young people to make friends
- Mysql database remote access permission settings
- UVM learning - build a simple UVM verification platform
- UWA report uses tips. Did you get it? (the fourth bullet)
- 全网显示 IP 归属地,是怎么实现的?
猜你喜欢
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Leetcode+ 76 - 80 storm search topic
13. Semaphore critical zone protection
从MediaRecord录像中读取H264参数
Hdu1236 ranking (structure Sorting)
Analysis of hot spots in AI technology industry
4. Random variables
shell编程01_Shell基础
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)
Jsp webshell Free from killing - The Foundation of JSP
随机推荐
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
Redis set password
HDU1228 A + B(map映射)
UVM learning - object attribute of UVM phase
Set the playback speed during the playback of UOB equipment
Session cookies and tokens
[Fantasy 4] introduction and use of UMG components (under update...)
PCL之K-d树与八叉树
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
[Lua] summary of common knowledge points (including common interview sites)
14. Code implementation of semaphore
Disassembling Meitu SaaS: driving the plane to change the engine
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
MYSQL环境配置
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
12. Process synchronization and semaphore
UVM learning - build a simple UVM verification platform
从MediaRecord录像中读取H264参数
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
What is the significance of the college entrance examination