How to eliminate duplicates while looping through JSON array
本问题已经有最佳答案,请猛点这里访问。
我将从服务器得到这个JSON作为响应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var responsefromserver = { "Restaurants": [ { "RestrntArea":"Home" }, { "RestrntArea":"Office" }, { "RestrntArea":"Office" } ] }; |
请告诉我,在前端循环时如何消除上面的重复项??
这是我的琴
http://jsfiddle.net/bremw/720/
试试这个。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var responsefromserver = { "Restaurants": [{ "RestrntArea":"Home" }, { "RestrntArea":"Office" }, { "RestrntArea":"Office" }] }; var html = []; for (var i = 0; i < responsefromserver.Restaurants.length; i++) { var name = responsefromserver.Restaurants[i].RestrntArea; if(html.indexOf(name)==-1) html.push(name); } alert(html.join('')); |