使用append方法将对象转换为字符串
On my page I have a section containing UL's and each UL gets populated from an array of strings
在我的页面上,我有一个包含UL的部分,每个UL都是从字符串数组中填充的
dealers = [{
title : "Brecksville",
address : "8383 Chippewa Rd.,",
locale : "Brecksville, Ohio - 44141",
phone : "(440) 740-0535",
location: {lat: 41.32134300, lng: -81.62338300}
}];
For brevity I only show one of the strings in the array of strings...
为了简洁,我只显示字符串数组中的一个字符串……
This string is populated into the UL like so...
这个字符串像这样填充到UL中…
function populatDealerSummaryList(select, dealers) {
$listSelector = $("#dealersList"); //The Dealer Summary List
$.each(dealers, function (i, obj) {
$listSelector.append(
"<ul class='dealer-summary-list'>" +
"<li class='dealerName'>" + obj.title + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.address + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.locale + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.location + "</li>" +
"<li class='dealer-summary-listItem'>" + obj.phone + "</li>" +
"</ul>");
});
}
above...the latitude and longitude are represented by obj.location...
以上……纬度和经度由object .location…
the problem is...the lat long only renders as [object, object]
问题是……lat只显示为[object, object]
I'm not sure how to pass it correctly to get it to render...please help
我不知道如何正确地传递它以使它呈现…请帮助
2 个解决方案
#1
4
You get this result because Object
method toString()
return this output string "[object Object]"
;
得到这个结果是因为Object方法toString()返回输出字符串“[Object]”;
You can write like this:
你可以这样写:
...>" + JSON.stringify(obj.location) + "<...
Or
或
...>" + obj.location.lat + ', ' + obj.location.lng + "<...
Look at JSFiddle. There are both approaches
看看JSFiddle。有两种方法
更多相关文章
- $.each遍历JSON字符串和 Uncaught TypeError: Cannot use 'in' o
- 如何在python中使用命名组和datadict从正则表达式模式中组合字符
- 字符串压缩 牛客网 程序员面试金典 C++ Python
- Python 用hashlib求中文字符串的MD5值
- 将字节列表转换为字节字符串
- [D]用python提取多段字符串该怎么写正则表达式。
- 如何用位于括号外的逗号分隔字符串?
- 用于搜索和替换大字符串的最快Python方法
- python 字符串操作