当前位置:网站首页>Take you to learn the new methods in Es5
Take you to learn the new methods in Es5
2020-11-06 20:37:29 【Tell me Zhan to hide】
List of articles
1. ES5 Some new methods are added in , Can be very convenient operation array or string , These methods mainly include the following aspects
- Array methods
- String method
- Object methods
2. Array methods
Iterative traversal method :forEach()、map()、filter()、some()、every()
Judgment method :isArray()
2.1 forEach Follow jQuery Of each Usage is similar. . Grammar is :
array.forEach(function(currentValue, index, arr))
- currentValue: Array the values of the current item
- index: Array the index of the current item
- arr: Array object itself
var arr = [1,2,3]
arr.forEach(function(value, index, array){
console.log(' Every array element '+ value)
console.log(' The index value of each array element '+ index)
console.log(' The array itself '+ array)
})
The operation effect is shown below
2.2 map()
array.map(function(currentValue, index, arr))
map() Method to create a new array , The result is the result returned after each element in the array invokes a supplied function .
Be careful :map() Method is to return a new array directly
- currentValue: Array the values of the current item
- index: Array the index of the current item
- arr: Array object itself
var array1 = [1, 4, 9, 16];
var map1 = array1.map(function(value, index, arr) {
return value * 2
});
console.log(map1) //[2,8,18,32]
2.3 filter()
array.filter(function(currentValue, index, arr))
filter() Method is used to filter to create a new array , The elements in the new array are checked by checking all the eligible elements in the specified array , It is mainly used to filter arrays .
filter() Method is to return a new array directly
- currentValue: Array the values of the current item
- index: Array the index of the current item
- arr: Array object itself
var arr = [12,66,88]
// Choose greater than 20 Of
var newarr = arr.filter(function(value, index){
return value >=20
})
It can be abbreviated as var newarr = arr.filter( //[66, 88])
console.log(newArr) //[66, 88]
The preview effect is shown in the following figure
2.4 some()
array.some(function(currentValue, index, arr))
some() Method is used to detect whether the elements in the array meet the specified conditions , Popular point to find whether there are elements in the array that meet the conditions
Be careful : some() Method return value is Boolean , If you find this element , Just go back to true, If not found, it will return false
If you find the first element that satisfies the condition , Then stop the cycle , No more searching
- currentValue: Array the values of the current item
- index: Array the index of the current item
- arr: Array object itself
var arr=[10,30,4]
var b = arr.some(function(value){
return value > 20
})
console.log(b) //true
2.5 every()
array.every(function(currentValue, index, arr))
every() Method to test whether all elements in an array can pass the test of a specified function . It returns a Boolean value .
Be careful :every() Method must be qualified for each element to return true, If one doesn't match , Then return to false.
If you receive an empty array , This method returns in all cases true.
- currentValue: Array the values of the current item
- index: Array the index of the current item
- arr: Array object itself
2.6 isArray()
Array.isArray(obj)
Array.isArray() Used to determine if the value passed is a Array
- obj Is the value that needs to be tested . If it is an array, it returns true, Otherwise return to false
difference :
- filter It's looking for elements that satisfy the condition , It returns an array , And it's to return all the elements that meet the conditions
- some It's to find out whether the element that satisfies the condition exists , It returns a Boolean value , If you look for the first element that satisfies the condition , Just stop the cycle . If the array queries for the only element , use some The method is more suitable , Because it finds this element , It's not a cycle , More efficient
- every It's to find out whether all elements meet the criteria , It returns a Boolean value
- map Is to create a new array , The result is the result returned after each element in the array invokes a supplied function
3. String method
trim() Method to remove white space characters from both ends of a string
str.trim()
trim() Method does not affect the original string itself , It returns a new string .
4. Object methods
Object.defineProperty() Define new properties or modify existing properties in the object
Object.defineProperty(obj, prop, descriptor)
- obj: It's necessary . Target audience
- prop: It's necessary . The name of the property to be defined or modified
- descriptor: It's necessary . Properties owned by the target property
Object.defineProperty() The third parameter descriptor explain : In the form of objects {} Writing
- value: Set the value of the property , The default is undefined
- writable: Whether the value can be overridden .true | false The default is false
- enumerable: Whether the target property can be enumerated .true | false The default is false
- configurable: Whether the target attribute can be deleted or the attribute can be modified again true | false The default is false
var obj = {
id: 1,
pname: ' millet ',
price: 1999
}
// How previous objects add and modify properties
obj.num = 1000
obj.pirce = 99
console.log(obj)
//Object.deefineProperty() Define new properties or modify existing properties
Object.defineProperty(obj, 'num',{
value: 1000,
})
Object.defineProperty(obj, 'pricee',{
value: 9.9,
})
Object.defineProperty(obj, 'id',{
// If the value is false, It is not allowed to modify
writable: false,
})
Object.defineProperty(obj, 'address',{
value: ' Zhengzhou, China ',
// If the value is false, Traversal is not allowed , enumeration
enumerable: false
})
console.log(obj)
console.log(Object.keys(obj))
summary
This article mainly shares ES5 Some new methods , There are array methods 、 String method 、 Object methods and so on , It mainly shares the usage of GE's method , Characteristics and so on .
版权声明
本文为[Tell me Zhan to hide]所创,转载请带上原文链接,感谢
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World