嗨我有一個腳本解析本地JSON對象(目前只是爲了顯示一個列表)。我怎樣才能只選擇一些JSON對象?
function generateFamilySelect() {
var implantData = JSON.parse(localStorage.getItem("implantData"));
var implantFamilies = "";
$.each(implantData.implantFamilies, function(index, value) {
implantFamilies += implantData.implantFamilies[index].familyDisplay + "<br />";
});
$("#holderForFamilySelect").html(implantFamilies);
}
和JSON對象:
{"implantFamilies":[
{"id":"1","familySelector":"aa","familyDisplay":"One","loadInitially":"1"},
{"id":"2","familySelector":"bb","familyDisplay":"Two","loadInitially":"1"},
{"id":"3","familySelector":"cc","familyDisplay":"Three","loadInitially":"1"},
{"id":"4","familySelector":"dd","familyDisplay":"Four","loadInitially":"0"},
{"id":"5","familySelector":"ee","familyDisplay":"Five","loadInitially":"0"},
{"id":"6","fami…
目前,列表顯示所有的元素。我如何修改此腳本以僅顯示"loadInitially":"1"
?
此外,快速的語法問題,我覺得行
implantFamilies += implantData.implantFamilies[index].familyDisplay + "<br />";
可以寫成像
implantFamilies += this[index].familyDisplay + "<br />";
,但我不能得到那個工作...
你必須在之前檢查這種情況添加到變量'implantFamilies' –
正如一個註釋:當你使用JSON字符串時,一旦你解析它,你有一個JavaScript對象。有沒有這樣的事情作爲JSON對象;) –
澄清@JeremyJStarcher說。 JSON字面意思是「Javascript Object Notation」。當然,它的迂腐。我甚至經常聽到它,但類似於說「ATM機」。首字母縮略詞已經說明它是一個對象,就像ATM的首字母縮寫詞已經聲明它是一臺機器。另見:「PIN號碼」等 –