<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'test3.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script src="js/jquery-2.1.1.min.js"></script> <script> // var obj=new Object(); // obj.name='z3'; // obj.age='21'; // alert(obj.name); // obj.name='w5'; // alert(obj.name); //模擬map //在js對象中key是永遠不會重複的 //把數組轉換成對象 //把數組中的值變成js對象的key //將js對象還原成數組 var array=[1,2,3,4,5,6,7,6,5,4]; function toObj(array){ var obj={}; var j=array.length; for(var i=0;i<j;i++){ obj[array[i]]=true; //重點在於key,value值不重要,隨便給一個 } return obj; } function tranToArray(obj){ var array=[]; for(var attr in obj){ if(obj.hasOwnProperty(attr)){ array.push(attr); } } return array; } function unique(array){ return tranToArray(toObj(array)); } alert(array); alert(unique(array)); </script> </head> <body> This is my JSP page. <br> </body> </html>