js遍历map(js遍历map对象)

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,要先导入一个HashMap.js文件 
没要求,引入这个文件之后,可以直接使用hashmap了var map = {'a': 'aaaaa','b': 'bbbbb'};for(var k in map){alert(map[k]);}可以使用jquery中的each()函数。
$.each(obj, function(i) {
alert(obj[i]);
});  
function 也可以写为function(key,value){
 }
key,value 就是map的key, valuejavascript中没有什么map

只有对象和数组可以遍历//你的JSON对象

var o = {

foo:"bar",

arr:[1,2,3],

subo: {

foo2:"bar2"

}

//处理每一项的键和值的函数

function process(key,value) {

document.body.innerHTML += key + " : " + value + "
";

//遍历函数

function traverse(o,func) {

for (i in o) {

func.apply(this,[i,o[i]]);

if (typeof(o[i])=="object") {

//递归

traverse(o[i],func);

}

//现在开始遍历

traverse(o, process);

js遍历map(js遍历map对象)_js遍历map(js遍历map对象)

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代码

struts2中的map遍历总结

1.map中的value为String字符串

key:

value:

******************************************

2.map中的value为Student对象

key=value ID num name sex age

3.map中的value为String数组

key=value ID num name sex age

4.map中的value为list集合

class ID num name sex age

${item.key}中的key的值是你定义的一个班级Class对象,它不是可遍历的对象。

${var.name}

${item.key.name}

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
  1. 本站完全免费,所有资源仅用于学习,如需商用,请购买版权
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
  2. 本站为非经营性网站,没有任何付费项目
    本站为非经营性网站,如果觉得我们网站好,请转发到朋友圈让更多的人知道。
  3. 本站站长联系方式
    站长邮箱 liujun100@vip.qq.com
    商业合作 service@yuucn.cn
    投诉举报 jb@yuucn.cn

    我们这个网站不卖,请不要幻想着收购我们。

评论(0)

提示:请文明发言