js中Map藏值制作table表格数据原理

时间:2025-04-07 10:29:14
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript" src="js/"></script> </head> <script> var map = new Map(); //声明一个map 用来存储 function setMap(id,name,flag) { if(flag) { var obj={ id:id, name:name };//定义数组存储数据 map.set(id,obj); } else{ map.delete(id); } console.info(map); //遍历获取所有的名称 var names = new Array();//定义names数组 var ids = new Array();//定义ids数组 map.forEach(function(item, key) { names.push(item.name); ids.push(key); }); } //子页面获取map中的值 function getMap() { var arr = new Array(); map.forEach(function(item, key) { arr.push(item); }); return arr; } //map清空 function clearMap() { map.clear(); } $(function(){ //初始化测试 var flag=$("#test").is(':checked'); //flag属性标记 setMap("testid","testname",flag); var map=getMap(); console.log(map); clearMap();//数据清空 var map2=getMap(); console.log(map2); }); </script> <body> <input type="checkbox" id="test" value="test" checked="checked"/> </body> </html>