当前位置:网站首页>Es5 new method
Es5 new method
2022-07-25 23:27:00 【Dragon eyes】
1. Array methods forEach Traversal array
arr.forEach(function(value, index, array) {
// Parameter one is : Array elements
// The second parameter is : Index of array elements
// The third parameter is : The current array
})
// Equivalent to array traversal for loop no return value
2. Array methods filter Filter array
var arr = [12, 66, 4, 88, 3, 7];
var newArr = arr.filter(function(value, index,array) {
// Parameter one is : Array elements
// The second parameter is : Index of array elements
// The third parameter is : The current array
return value >= 20;
});
console.log(newArr);//[66,88] // The return value is a new array
3. Array methods some
some Find whether there are elements in the array that meet the conditions
var arr = [10, 30, 4];
var flag = arr.some(function(value,index,array) {
// Parameter one is : Array elements
// The second parameter is : Index of array elements
// The third parameter is : The current array
return value < 3;
});
console.log(flag);//false The return value is Boolean , As soon as an element satisfying the condition is found, the loop is terminated immediately
4. Filter product cases
- Define array object data
var data = [{
id: 1,
pname: ' millet ',
price: 3999
}, {
id: 2,
pname: 'oppo',
price: 999
}, {
id: 3,
pname: ' glory ',
price: 1299
}, {
id: 4,
pname: ' Huawei ',
price: 1999
}, ];
- Use forEach Traverse the data and render it to the page
data.forEach(function(value) {
var tr = document.createElement('tr');
tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>';
tbody.appendChild(tr);
});
Filter data by price
- Get the search button and bind the click event to it
search_price.addEventListener('click', function() {
});
- Use filter Filter out the price information entered by the user
search_price.addEventListener('click', function() {
var newDate = data.filter(function(value) {
//start.value Is the starting interval
//end.value Is the end of the interval
return value.price >= start.value && value.price <= end.value;
});
console.log(newDate);
});
Re render the filtered data to the table
- Encapsulate the logic of rendering data into a function
function setDate(mydata) {
// First empty the original tbody The data in it
tbody.innerHTML = '';
mydata.forEach(function(value) {
var tr = document.createElement('tr');
tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>';
tbody.appendChild(tr);
});
}
2. Re render filtered data
search_price.addEventListener('click', function() {
var newDate = data.filter(function(value) {
return value.price >= start.value && value.price <= end.value;
});
console.log(newDate);
// Render the filtered objects to the page
setDate(newDate);
});
Filter by product name
Get the product name entered by the user
Bind click event for query button , Filter the entered product name with this data
search_pro.addEventListener('click', function() {
var arr = [];
data.some(function(value) {
if (value.pname === product.value) {
// console.log(value);
arr.push(value);
return true; // return It must be written after true
}
});
// Render the data to the page
setDate(arr);
})
5.some and forEach difference
- If you query the only element in the array , use some More appropriate , stay some Inside encounter return true Is to stop traversal Iteration is more efficient
- stay forEach Inside return The iteration will not be terminated
6.trim Method to remove spaces at both ends of a string
var str = ' hello '
console.log(str.trim()) //hello Remove the spaces at both ends
var str1 = ' he l l o '
console.log(str.trim()) //he l l o Remove the spaces at both ends
7. Get the property name of the object
Object.keys( object ) Gets the property name in the current object , The return value is an array
var obj = {
id: 1,
pname: ' millet ',
price: 1999,
num: 2000
};
var result = Object.keys(obj)
console.log(result)//[id,pname,price,num]
8.Object.defineProperty
Object.defineProperty Set or modify properties in an object
Object.defineProperty( object , Modified or added property name ,{
value: Modify or add the value of the attribute ,
writable:true/false,// If the value is false It is not allowed to modify this property value
enumerable: false,//enumerable If the value is false Traversal is not allowed
configurable: false //configurable If false You are not allowed to delete this property Whether the property can be deleted or whether the property can be modified again
})
边栏推荐
- MVVM model
- Source code of YY music wechat applet imitating Netease cloud music
- WordPress function encyclopedia, you can do the theme after learning it
- 【MUDUO】打包EventLoop和Thread
- JS get the current date and time
- Mongodb query and projection operators
- js正则表达式匹配ip地址(ip地址正则表达式验证)
- The difference between MySQL clustered index and non clustered index
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
- PHP JSON variable array problem
猜你喜欢

WebMvcConfigurationSupport

Mongodb features, differences with MySQL, and application scenarios

Grain Academy p98 trample pit e.globalexceptionhandler: null

wordpress去掉网站发布时间

学习探索-波浪

多模态——Deep Multi-Modal Sets
![[code case] blog page design (with complete source code)](/img/9e/0e7cab956515b9cc75a7567eb477d2.png)
[code case] blog page design (with complete source code)

Source code of YY music wechat applet imitating Netease cloud music

Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download

How does Navicat modify the language (Chinese or English)?
随机推荐
Which securities firm is the best and safest for beginners to open an account
The VM session was closed before any attempt to power it on
The difference between MySQL clustered index and non clustered index
Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)
Serialize data type
Classes and objects (2) (6 default member functions)
日期类的实现
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
Drive board network cable directly connected to computer shared network configuration
物理防火墙是什么?有什么作用?
MES系统设备管理概述(下)
152. 乘积最大子数组-动态规划
How does PHP remove an element from an array based on the key value
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
Tencent map API request source is not authorized, this request source domain name
E-commerce RPA, a magic weapon to promote easy entry
Network Security Learning notes-1 file upload
R language drawing parameters (R language plot drawing)
Ffmpeg first learning (only for coding)
Flight control implementation of four rotor aircraft "suggestions collection"