当前位置:网站首页>Query commodity cases (operate data with array addition method) / key points
Query commodity cases (operate data with array addition method) / key points
2022-07-25 23:27:00 【Dragon eyes】
Code running rendering

Case description

Be careful : If you query the only element in the array , use some More appropriate , Because it finds this element , It's not a cycle , More efficient
stay forEach and filter Inside return true The iteration will not be terminated
stay some Meet inside return true Will terminate the iteration ( Traverse ), Iteration is more efficient
Case implementation code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style> table {
width: 400px; border: 1px solid #000; border-collapse: collapse; margin: 0 auto; } td, th {
border: 1px solid #000; text-align: center; } input {
width: 50px; } .search {
width: 600px; margin: 20px auto; } </style>
</head>
<body>
<div class="search">
Inquire according to the price : <input type="text" class="start"> - <input type="text" class="end"> <button class="search-price"> Search for </button> Query by commodity name : <input type="text" class="product"> <button class="search-pro"> Inquire about </button>
</div>
<table>
<thead>
<tr>
<th>id</th>
<th> The product name </th>
<th> Price </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script> // Use the new array method to manipulate 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 }, ]; // 1. Get the corresponding element var tbody = document.querySelector('tbody'); var search_price = document.querySelector('.search-price'); var start = document.querySelector('.start'); var end = document.querySelector('.end'); var product = document.querySelector('.product'); var search_pro = document.querySelector('.search-pro'); // Call the default first data, Prevent loading without data setData(data); // Render the data to the page function setData(mydata) {
// Empty first tbody The data in it tbody.innerHTML = ''; mydata.forEach(function(value) {
// console.log(value); var tr = document.createElement('tr'); tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>'; tbody.appendChild(tr); }); } // 3. Query goods by price // When we hit the button , We can filter the objects in the array according to our commodity price search_price.addEventListener('click', function() {
var newData = data.filter(function(value) {
return value.price >= start.value && value.price <= end.value; }) console.log(newData); // Render the filtered objects to the page setData(newData); }); // 4. Find products by product name // If you query the only element in the array , use some More appropriate , Because it finds this element , It's not a cycle , More efficient ] search_pro.addEventListener('click', function() {
var arr = []; // because some The output is Boolean , Let's define an array here data.some(function(value) {
if (value.pname == product.value) {
// console.log(value); arr.push(value); // Add to arr In this array return true; // It must be written here true } }); // Render the data to the page setData(arr); }) </script>
</body>
</html>
边栏推荐
- 1913. 两个数对之间的最大乘积差-无需排序法
- 数组中重复的数字
- OASYS system of code audit
- MES系统设备管理概述(下)
- PyTorch的数据输入格式要求及转换
- Multimodal deep multi modal sets
- 谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
- Mongodb features, differences with MySQL, and application scenarios
- Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"
- Tips for using (1)
猜你喜欢

@Import

The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet

行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化

BI 系统中为什么会有很多快照表?

Drive board network cable directly connected to computer shared network configuration

OASYS system of code audit

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

【代码案例】博客页面设计(附完整源码)

Why are there many snapshot tables in the BI system?
随机推荐
asp日期函数(磁盘函数不正确怎么办)
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
JS get the current date and time
Mongodb update operator (modifier)
Pytorch data input format requirements and conversion
四旋翼飞行器的飞控实现「建议收藏」
ETL tool (data synchronization) II
[QNX hypervisor 2.2 user manual]9.6 GDB
WebMvcConfigurationSupport
@Import
The difference between MySQL clustered index and non clustered index
5 ROS simulation modeling (3-rviz+gazebo+ control simulation robot)
TS function
WordPress removes the website publishing time
Strategy mode_
anaconda安装教程环境变量(如何配置环境变量)
理解的英文(言语理解)
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
Wrote a little webapi knowledge points from 0 to 1
电商RPA,大促轻松上阵的法宝