当前位置:网站首页>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>边栏推荐
- How does Alibaba use DDD to split microservices?
- YUV to uiimage
- 为什么md5不可逆,却还可能被md5免费解密网站解密
- FPGA: use PWM wave to control LED brightness
- Interpretation of afnetworking4.0 request principle
- Autoreleasepool problem summary
- RT based_ Distributed wireless temperature monitoring system of thread (I)
- Dcgan:deep volume general adaptive networks -- paper analysis
- FreeRTOS learning (I)
- MySQL(5)
猜你喜欢

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

The go zero singleton service uses generics to simplify the registration of handler routes

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

RT_ Use of thread message queue

阿里怎么用DDD来拆分微服务?

Reading notes of SMT practical guide 1

类和对象【中】

Online sql to XML tool

Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"

驾驭EVM和XCM的强大功能,SubWallet如何赋能波卡和Moonbeam
随机推荐
Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"
【ARXIV2203】SepViT: Separable Vision Transformer
[high CPU consumption] software_ reporter_ tool.exe
Implementation of simple upload function in PHP development
PC端-bug记录
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
【计算机三级信息安全】信息安全保障概述
Keil Chinese garbled code solution
SMD component size metric English system corresponding description
Check box error
Clickhouse pit filling note 2: the join condition does not support non equal judgments such as greater than and less than
How to quickly locate bugs? How to write test cases?
PC side bug record
Introduction to testcafe
MySQL date and time function, varchar and date are mutually converted
MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?
【CVPR2022 oral】Balanced Multimodal Learning via On-the-fly Gradient Modulation
7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
11. < tag dynamic programming and subsequence, subarray> lt.115. Different subsequences + Lt. 583. Deletion of two strings DBC
Automated test tool playwright (quick start)