当前位置:网站首页>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>边栏推荐
- Flink mind map
- Service object creation and use
- Offline loading of wkwebview and problems encountered
- Implementation of simple upload function in PHP development
- MySQL(5)
- Visual studio 2019 new OpenGL project does not need to reconfigure the environment
- The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
- Know etcd
- Interpretation of afnetworking4.0 request principle
- [learning record] data enhancement 1
猜你喜欢
![[high CPU consumption] software_ reporter_ tool.exe](/img/3f/2c1ecff0a81ead0448e1215567ede7.png)
[high CPU consumption] software_ reporter_ tool.exe

The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?

Mysql基本查询

Duoyu security browser will improve the security mode and make users browse more safely

HashSet add

What is the core value of testing?

How practical is the struct module? Learn a knowledge point immediately

PC side bug record

mysql的日期与时间函数,varchar与date相互转换

微服务故障模式与构建弹性系统
随机推荐
I interviewed a 38 year old programmer and refused to work overtime
PC side bug record
在外包公司两年了,感觉快要废了
Online sql to XML tool
Service object creation and use
How to successfully test php7.1 connecting to sqlserver2008r2
FreeRTOS learning (I)
Using RAC to realize the sending logic of verification code
Simulink automatically generates STM32 code details
【ARXIV2203】Efficient Long-Range Attention Network for Image Super-resolution
数据库日期类型全部为0
php7.1 连接sqlserver2008r2 如何测试成功
How to quickly locate bugs? How to write test cases?
Pipe /createpipe
在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子
The go zero singleton service uses generics to simplify the registration of handler routes
Microservice failure mode and building elastic system
【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
Tips for using swiper (1)
为什么md5不可逆,却还可能被md5免费解密网站解密