我试图剖析JSON数据以执行以下操作:
当前问题:
我已将项目上传到GitHub:https://github.com/adamkwadsworth/Audley-Travel
JSON - https://github.com/adamkwadsworth/Audley-Travel/blob/master/json/locations.json
有问题的代码:
function filterData(){
var rawdata = jsonData;
//console.log(rawdata);
var data = [];
for (var index = 0; index < rawdata.length; index++) {
var item = rawdata[index];
var item1 = {};
var type1 = {}, type2 = {}, type3 = {}, type4 = {}, type5 = {}, type6 = {}, type7 = {}, type8 = {}, type9 = {}, type10 = {};
item1.Country = item.Location;
type1["Activity/Adventure"] = item["Activity/ Adventure"];
type2["Beach Escape"] = item["Beach Escape"];
type3["City Break"] = item["City Break"];
type4["Food/Culture"] = item["Food/Culture"];
type5["Visiting Friends/Family"] = item["Visiting Friends/Family"];
type6["Great Train/Boat Journey"] = item["Great Train/Boat Journey"];
type7.Scenery = item.Scenery;
type8.Sightseeing = item.Sightseeing;
type9["Theme Park"] = item["Theme Park"];
type10["Wildlife/Safari"] = item["Wildlife/Safari"];
var types = [type1, type2, type3, type4, type5, type6, type7, type8, type9, type10];
var items = [item1, types];
data.push(items);
var filterdata1 = JSON.stringify(item1);
$('.filterlist').append('<li><span>1</span>'+filterdata1+'</li>');
}
console.log(data);
答案 0 :(得分:0)
虽然您已经使用jQuery专门标记了这个问题,但我将提供另一种方法:改为使用MVVM库。
通过稍微重构数据存储,这可以变成非常简单的代码,例如,Vue.js:
var items = {
'Activity/Adventure': ['USA', 'Canada', 'Russia'],
'Beach Escape': ['Mexico', 'Egypt'],
'Food': ['Italy', 'France']
};
new Vue({
el: 'body',
data: {
selected: Object.keys(items)[0],
items: items
}
});
ul {
list-style-type: decimal;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.12/vue.min.js"></script>
<select v-model="selected">
<option v-repeat="items">{{$key}}</option>
</select>
<ul>
<li v-repeat="item: items[this.selected]">{{item}}</li>
</ul>