---
title: "js取el中的map值及js replaceAll实现"
author: "jslfl"
date: "2013-04-10T15:55:31Z"
tags: []
categories: ["Web前端"]
slug: "js-el-map-js-replaceall"
draft: false
lastmod: "2013-04-10T15:55:31Z"
id: 1845155
url: "https://jslfl.iteye.com/blog/1845155"
---

<%

Map map = new HashMap();
map.put("20",0);
map.put("21",1);
request.setAttribute("map",map);

%>

js中不能直接如下取值
var key = "21";
alert("${map}");//想直接实现这种取值效果,不行



下面通过转成json来实现

<script type="text/javascript">
String.prototype.replaceAll = function(s1,s2) { 
    return this.replace(new RegExp(s1,"gm"),s2); 
}

var str = "${map}";
str = str.replaceAll("=",":");


var map = eval("(" + str + ")");
alert(map);


</script>