web123456

Principle of Map hiding values ​​in js to create table table data

<!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>Untitled document</title> <script type="text/javascript" src="js/"></script> </head> <script> var map = new Map(); //Declare a map to store function setMap(id,name,flag) { if(flag) { var obj={ id:id, name:name };//Define array to store data map.set(id,obj); } else{ map.delete(id); } console.info(map); //Travel to get all names var names = new Array();//Define the names array var ids = new Array();//Define ids array map.forEach(function(item, key) { names.push(item.name); ids.push(key); }); } //Subpage gets the value in the map function getMap() { var arr = new Array(); map.forEach(function(item, key) { arr.push(item); }); return arr; } //Clear map function clearMap() { map.clear(); } $(function(){ //Initialize the test var flag=$("#test").is(':checked'); //flag attribute tag setMap("testid","testname",flag); var map=getMap(); console.log(map); clearMap();// Clear data var map2=getMap(); console.log(map2); }); </script> <body> <input type="checkbox" id="test" value="test" checked="checked"/> </body> </html>