草庐IT

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

叶涛网站推广优化 2023-04-09 原文

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);

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=valueIDnumnamesexage

3.map中的value为String数组

key=valueIDnumnamesexage

4.map中的value为list集合

classIDnumnamesexage

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

${var.name}

${item.key.name}

有关js遍历map(js遍历map对象)的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  5. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  6. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  7. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  8. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  9. ruby - 一个 YAML 对象可以引用另一个吗? - 2

    我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的ruby​​yaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir

  10. ruby - 更改 ActiveRecord 中对象的类 - 2

    假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。

随机推荐