我有以下陣列:對象數組返回對應特定的ID標籤
"facilities":[
{
"name": "Gratis",
"id": "FACILITYCATEGORYFREE",
"facilities":[
{"name": "Free city maps", "id": "FREECITYMAPS"},
{"name": "Free parking", "id": "FREEPARKING"},
{"name": "Free Wifi", "id": "FREEWIFI"}
]
}
]
我需要在一個特定的順序追加內的「UL」的免費設施:
- 免費WIFI
- 免費的城市地圖
我有以下幾點:
for (i =0; i < json.facilities[0].facilities.length; i++) {
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
然而,這將只打印基於它們所處的位置在數組中
我都試過,但現在還沒有:
for (i =0; i < json.facilities[0].facilities.length; i++) {
if(json.facilities[0].facilities[i].name.length && json.facilities[0].facilities[i].id === 'FREEWIFI'){
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
if (json.facilities[0].facilities[i].id === 'BREAKFASTINCLUDED') {
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
if (json.facilities[0].facilities[i].id === 'FREEINTERNETACCESS') {
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
if (json.facilities[0].facilities[i].id === 'TOWELSINCLUDED') {
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
if (json.facilities[0].facilities[i].id === 'LINENINCLUDED') {
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
if (json.facilities[0].facilities[i].id === 'FREECITYMAPS') {
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
if (json.facilities[0].facilities[i].id === 'FREEPARKING') {
$("#facilities").append("<li class='fa fa-check'><span>" + json.facilities[0].facilities[i].name + "</span></li>");
}
}
只是檢查是否'json.facilities [0] .facilities [I] .name'包含你正在尋找的文本,並採取相應的行動 –
你需要的所有追加他們還是隻有那兩個? – FredMaggiowski
所有這些都按特定順序排列 – Alex