【问题标题】:jquery .map() return object inside mapjquery .map() 返回地图内的对象
【发布时间】:2013-02-16 22:00:58
【问题描述】:

嘿嘿,

我只是在构建某种地理编码器,我需要将 json-map 发送到 groovy 后端,

试图映射这个 ul 结构 : http://jsfiddle.net/PJFVN/1/

<ul id="hiddenmap">
<li  class="hidden" id="street">Westerwarft 2</li>
<li  class="hidden" id="plz">25859</li>
<li  class="hidden" id="city">Hallig Hooge</li>
<li  class="hidden" id="country" value="" >DE</li>
<li  class="hidden" id="lon" > 8.516472</li>
<li  class="hidden" id="lat" >54.577993</li>
</ul>

到一张看起来像的地图

[{"street":"Westerwarft 2","plz":"25859","location":{"lat":"54.577993","lon":" 8.516472"},"country":"DE"}]

使用以下js:

var map =  $('#hiddenmap').map(function() {
var $item = $(this);
var loc =   '{"lat":"'+$item.find('li#lat').text()+'","lon":"'+$item.find('li#lon').text()+'"},';
return {  
street: $item.find('li#street').text(),
plz: $item.find('li#plz').text(),
location:loc ,
country: $item.find('li#country').text()
};
}).get();
var v = JSON.stringify(map); 
alert(v);

但正如你在小提琴中看到的那样,我的肮脏尝试抛出了

[{"street":"Westerwarft 2","plz":"25859","location":"{\"lat\":\"54.577993\",\"lon\":\" 8.516472\"},","country":"DE"}]

所以我需要一种本地方式来获取地图内的位置对象, 从角度来看,我需要在地图上加入更多地址

有没有办法做到这一点?

因为目前我正在重复自己,为每个看起来很糟糕的不同情况手动构建地图:

       var String = '['+'{'+'"fax":'+'"'+fax1+'"'+','+'"genau":'+genau1+','+'"land":'+'"'+land1+'"'+','+'"location": {'+'"lon":'+lon1+','+'"lat":'+lat1+'},'+'"notruf":'+'"'+notruf1+'"'+','+'"ort":'+'"'+ort1+'"'+','+'"plz":'+'"'+plz1+'"'+','+'"strasse":'+'"'+strasse1+'"'+','+'"telefon":'+'"'+telefon1+'"},{'+'"fax":'+'"'+fax2+'"'+','+'"genau":'+genau2+','+'"land":'+'"'+land2+'"'+','+'"location": {'+'"lon":'+lon2+','+'"lat":'+lat2+'},'+'"notruf":'+'"'+notruf2+'"'+','+'"ort":'+'"'+ort2+'"'+','+'"plz":'+'"'+plz2+'"'+','+'"strasse":'+'"'+strasse2+'"'+','+'"telefon":'+'"'+telefon2+'"}]';

我需要摆脱这个

提前感谢任何提示

【问题讨论】:

    标签: jquery json map return


    【解决方案1】:

    也许我没有正确理解您的问题,但我不确定您是否为该位置手动构建字符串。你为什么不把它变成一个原生对象,让 JSON.stringify() 处理转换。

    像这样:

    var map = $('#hiddenmap').map(function () {
        var $item = $(this);
        var loc = {
            lat: $item.find('li#lat').text(),
            lon: $item.find('li#lon').text()
        };
        return {
            street: $item.find('li#street').text(),
            plz: $item.find('li#plz').text(),
            location: loc,
            country: $item.find('li#country').text()
        };
    }).get();
    var v = JSON.stringify(map);
    console.log(v);
    alert(v);
    

    【讨论】:

    • 可以,非常感谢。我的大脑只是看着我一年前所做的事情,让它变得尽可能复杂
    【解决方案2】:

    只需将 loc 变量定义为真正的 JSON 映射,而不是表示 JSON 映射的字符串:http://jsfiddle.net/PJFVN/2/

    jQuery(document).ready(function ($) {
        $('#action').click(function () {
            var map = $('#hiddenmap').map(function () {
                var $item = $(this);
                var loc = {lat : $item.find('li#lat').text(), 
                           lon : $item.find('li#lon').text()};
                return {
                    street: $item.find('li#street').text(),
                    plz: $item.find('li#plz').text(),
                    location: loc,
                    country: $item.find('li#country').text()
                };
            }).get();
            var v = JSON.stringify(map);
            alert(v);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-25
      • 2013-11-28
      • 2021-08-18
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      相关资源
      最近更新 更多