看看:
https://jsfiddle.net/3fmp43db/
data = [{
city: 'Mushroom Kingdom',
}, {
city: 'Mushroom Kingdom',
}, {
city: 'Mushroom Kingdom',
}, {
city: 'Mushroom Kingdom',
}, {
city: 'Mushroom Kingdom',
}, {
city: 'Mushroom Kingdom',
}, {
city: 'Planet Zebes',
}, {
city: 'Planet Zebes',
}, {
city: 'Planet Zebes',
}];
for(var i = 0; i < data.length; i++) {
var obj = data[i];
$(".test").append(obj.city + "<br>")
}
这是一个简单的问题,在呈现此列表之前,我无法弄清楚如何删除重复的值。
答案 0 :(得分:2)
从
更改最后一个循环for(var i = 0; i < data.length; i++) {
var obj = data[i];
$(".test").append(obj.city + "<br>")
}
到
var allCities = {};
for(var i = 0; i < data.length; i++) {
allCities[ data[i].city ] = "";
}
$(".test").append(Object.keys( allCities ).join("<br>"));
检查此更新的fiddle