当前位置:网站首页>What are the methods of array objects in Es5 and what are the new methods in ES6
What are the methods of array objects in Es5 and what are the new methods in ES6
2022-07-28 05:16:00 【M78_ Domestic 007】
stay ES5 Common methods of array objects in :
1、concat() Connect two or more arrays , And return the result
<script>
let arr=[1,2,3]
let arr1=[4,5,6]
var re = arr.concat(arr1);
console.log(re);//[1,2,3,4,5,6]
</script>2、join() Put all the elements of the array into a string , The element is divided by the specified separator
<script>
let arr=[1,2,3]
var re = arr.join("*")
console.log(re); //1*2*3
</script>3、pop() Delete and return the last element of the array , Similarly shift() Delete and return the first element of the array
<script>
let arr=[1,2,3]
var re = arr.pop()
console.log(re)//3
re1 = arr.shift()
console.log(re1); //1
</script>4、 push Add one or more elements... To the end of the array , And return the length of the new array , Similarly unshift() Add one or more elements to the header of the array , And returns the new length
<script>
let arr=[1,2,3]
var re = arr.push(4)
console.log(re)//4
re1 = arr.unshift(0)
console.log(re1); //5
console.log(arr) // [0,1,2,3,4]
</script>5、reverse() Reverse the order of the elements in the array
<script>
let arr=[1,2,3]
var arr1=arr.reverse()
co console.log(arr1)//[3,2,1]
</script>6、sort() Sort elements of an array
<script>
let arr=[1,8,3,42,3]
var arr1=arr.sort()
console.log(arr1)//[1,3,3,8,42]
</script>7、 splice() Delete several elements in the specified position , Then add new elements ; It can be seen as pop、push、shift、unshift The synthesis of these methods .
<script>
// As pop() when , Do not write the added number , Just delete
var arr=[10,30,40,5,6]
var re=arr.splice(4,1)// From the subscript 4 To delete 1 Elements
console.log(re,arr) //[6] [10,30,40,5]
// As push when , Don't delete , Just add
var arr=[10,30,40,5,6]
var re=arr.splice(4,0,7)// From the subscript 4 To delete 0 Elements , add to 7
console.log(re,arr) //[] [10,30,40,5,6,7]
//shift() unshift() similar
// comprehensive
var arr=[10,30,40,5,6]
var re=arr.splice(2,2,"hello","h5")
// From the subscript 2 To delete 2 Elements And insert here "hello","h5"
console.log(re,arr)//[40,5] [10,30,"hello","h5",6]
</script>8、slice() Returns the selected element from an existing array , There are two parameters , The first parameter is the starting subscript of the selection , The second parameter is the selected termination subscript , Terminated subscripts cannot be taken , Parameters can take negative numbers , among -1 The subscript of the last number .
<script>
var arr=[10,30,40,5,6]
var re=arr.slice(0,3)
console.log(re) //[10,30,40]
var re1=arr.slice(-3,-1)
console.log(re1) //[40,5]
</script>9、toString() Convert an array to a string , And return the result .
<script>
let arr=[1,2,3,5,6]
var re = arr.toString()
console.log(re);//1,2,3,5,6
</script>10、toLocaleString() Convert an array to a local string , And return the result , And toString() Basically the same , But notice two differences :1、 The number format is converted to the thousand separator 2、 Date format conversion
Specify :
<script>
let arr=[1,2,3,5,6]
var re = arr.toLocaleString()
console.log(re);//1,2,3,5,6
// difference 1:
let s=1111
s.toLocaleString(); // 1,111
s.toString(); // 1111
// difference 2:
const date = new Date();
date.toString(); // Mon Jul 11 2022 21:27:34 GMT+0800 ( China standard time )
date.toLocaleString(); // 2022/7/11 21:28:08
</script>11、valueof() Returns the original value of the array object
<script>
var arr=[10,80,30,20]
console.log(arr.valueOf()) //[10,80,30,20]
</script>ES6 New methods for array objects in :
1、find(): Find the elements in the array that match the criteria , If there are more than one element that meets the criteria , Then return the first element .
<script>
let arr = Array.of(1, 2, 3, 4);
console.log(arr.find(item => item > 2)); // 3
// Writing method of ordinary function
console.log(arr.find(function(el){
if(el>2){
return true
}
})); //3
</script>2、findIndex(): Find the index of the eligible elements in the array , If there are more than one element that meets the criteria , Returns the first element index , And find The method is similar .
3、fill(): Fills the contents of array elements with a range of indexes into a single specified value .
<script>
let arr = Array.of(1, 2, 3, 4);
// Parameters 1: The value to fill
// Parameters 2: The starting index to be filled
// Parameters 3( Optional ): Filled end index , The default is the end of the array
console.log(arr.fill(0,1,2)); // [1, 0, 3, 4]
</script>4 、 entrys(): Traversal key value pairs ( The index and elements of the array are traversed as a group )、keys(): Traversal keys 、values(): Traverse the key values ( understand )
5、includes(): Whether the array contains the specified value . Be careful : And Set and Map Of has Method of differentiation ;Set Of has Method is used to find the value ;Map Of has Method is used to find the key name .
<script>
var arr=[1,,22,3,4,5,,6]
var re=arr.includes(2)
console.log(re); //false
</script>6、flat() Used to reduce the dimension of an array , The parameters in parentheses specify the number of nesting levels of the transformation , Default not to write down one dimension .
<script>
console.log([1 ,[2, 3]].flat()); // [1, 2, 3]
console.log([1 ,[2, 3,[22,33,[44,55]]]].flat(2)); // [1, 2, 3,22,33,[44,55]]
</script>simulation flat Method , Realization flat The function of :
<script>
var arr=[[10,20,30],40,50,[[60,70],80,[90,[110],100]]]
Array.prototype.myflat=function(num=1){
var arr=[]
for(var i=0;i<this.length;i++){
if(this[i].constructor==Array&&num>0){
var arr1=this[i].myflat(num-1)
for(var j=0;j<arr1.length;j++){
arr.push(arr1[j])
}
}else{
arr.push(this[i])
}
}
return arr
}
var re2=arr.myflat(1)
console.log(re2) //[10, 20, 30, 40, 50,[60,70], 80,[90,[110],100]]
</script>边栏推荐
- Microservice failure mode and building elastic system
- Share several methods of managing flag bits in C program
- [internal mental skill] - creation and destruction of function stack frame (C implementation)
- POJ 2763 housewife wind (tree chain partition + edge weighting point weight)
- 【ARIXV2204】Neighborhood attention transformer
- php7.1 连接sqlserver2008r2 如何测试成功
- How to simulate common web application operations when using testcafe
- Transformer -- Analysis and application of attention model
- Visual studio 2019 new OpenGL project does not need to reconfigure the environment
- Making RPM packages with nfpm
猜你喜欢

Data security is gradually implemented, and we must pay close attention to the source of leakage

RT_ Use of thread message queue

Program life | how to switch to software testing? (software testing learning roadmap attached)

【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution

PC端-bug记录

C language classic 100 question exercise (1~21)

【ARXIV2203】CMX: Cross-Modal Fusion for RGB-X Semantic Segmentation with Transformers

【CVPR2022】Multi-Scale High-Resolution Vision Transformer for Semantic Segmentation

【CVPR2022】On the Integration of Self-Attention and Convolution

如何在 FastReport VCL 中通过 Outlook 发送和接收报告?
随机推荐
Why is MD5 irreversible, but it may also be decrypted by MD5 free decryption website
Clickhouse pit filling note 2: the join condition does not support non equal judgments such as greater than and less than
How to simulate common web application operations when using testcafe
Dell remote control card uses ipmitools to set IPMI
RT_ Use of thread mailbox
Message forwarding mechanism -- save your program from crashing
分享几种管理C程序中标志位的方法
HashSet add
Read the paper -- a CNN RNN framework for clip yield prediction
Interpretation of afnetworking4.0 request principle
list indices must be integers or slices, not tuple
Automated test tool playwright (quick start)
凛冬已至,程序员该怎么取暖
7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
Gym 101911c bacteria (minimum stack)
HDU 3585 maximum shortest distance
HDU 1530 maximum clique
数据库日期类型全部为0
使用nfpm制作rpm包
SMD component size metric English system corresponding description