javascript怎么遍歷map
var?map?=?new?HashMap();
map.put(a,1);
map.put(b,2);
遍歷:
var?key?=?map.keySet();
for?(var?i?in?key){
?alert(map.get(key[i]));
}
注:js?中使用map,要先導(dǎo)入一個HashMap.js文件?
沒要求,引入這個文件之后,可以直接使用hashmap了var map = {'a': 'aaaaa','b': 'bbbbb'};for(var k in map){alert(map[k]);}可以使用jquery中的each()函數(shù)。
$.each(obj,?function(i)?{
alert(obj[i]);
});??
function?也可以寫為function(key,value){
?}
key,value?就是map的key,?valuejavascript中沒有什么map
只有對象和數(shù)組可以遍歷//你的JSON對象
var o = {
foo:"bar",
arr:[1,2,3],
subo: {
foo2:"bar2"
}
//處理每一項的鍵和值的函數(shù)
function process(key,value) {
document.body.innerHTML += key + " : " + value + "
";
//遍歷函數(shù)
function traverse(o,func) {
for (i in o) {
func.apply(this,[i,o[i]]);
if (typeof(o[i])=="object") {
//遞歸
traverse(o[i],func);
}
//現(xiàn)在開始遍歷
traverse(o, process);
jsp頁面如何對map集合遍歷
MapAction.java
Java代碼
package com.zx.demo.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import com.zx.demo.model.Product;
import com.zx.demo.model.Student;
public class MapAction extends ActionSupport
private Map map;
private Map studentMap;
private Map arrayMap;
private Map > listMap;
public String testMap()
{
map=new HashMap ();
map.put("1", "one");
map.put("2", "two");
studentMap=new HashMap ();
studentMap.put("student1",new Student(new Long(1),"20034140201","張三1","男",25));
studentMap.put("student2",new Student(new Long(2),"20034140202","張三2","女",26));
studentMap.put("student3",new Student(new Long(3),"20034140202","張三3","男",27));
arrayMap=new HashMap ();
arrayMap.put("arr1", new String[]{"1","2003401","leejie","male","20"});
arrayMap.put("arr2", new String[]{"2","2003402","huanglie","male","25"});
arrayMap.put("arr3", new String[]{"3","2003403","lixiaoning","male","21"});
listMap=new HashMap >();
List list1=new ArrayList ();
list1.add(new Student(new Long(1),"20034140201","張三1","男",25));
list1.add(new Student(new Long(2),"20034140202","張三2","男",25));
list1.add(new Student(new Long(3),"20034140203","張三3","男",25));
listMap.put("class1", list1);
List list2=new ArrayList ();
list2.add(new Student(new Long(1),"20034140301","李四1","男",20));
list2.add(new Student(new Long(2),"20034140302","李四2","男",21));
list2.add(new Student(new Long(3),"20034140303","李四3","男",22));
list2.add(new Student(new Long(4),"20034140304","李四4","男",23));
listMap.put("class2", list2);
return SUCCESS;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Map getStudentMap() {
return studentMap;
}
public void setStudentMap(Map studentMap) {
this.studentMap = studentMap;
}
public Map getArrayMap() {
return arrayMap;
}
public void setArrayMap(Map arrayMap) {
this.arrayMap = arrayMap;
}
public Map > getListMap() {
return listMap;
}
public void setListMap(Map > listMap) {
this.listMap = listMap;
}
2.testMap.jsp
Java代碼
1.map中的value為String字符串
key:
value:
******************************************
2.map中的value為Student對象
key=value | ID | num | name | sex | age |
3.map中的value為String數(shù)組
key=value | ID | num | name | sex | age |
4.map中的value為list集合
class | ID | num | name | sex | age |
${item.key}中的key的值是你定義的一個班級Class對象,它不是可遍歷的對象。
${var.name}文章來源:http://www.zghlxwxcb.cn/news/detail-426233.html
${item.key.name}文章來源地址http://www.zghlxwxcb.cn/news/detail-426233.html
到了這里,關(guān)于js遍歷map(js遍歷map對象)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!