我有以下的JSON從PHP腳本如何循環瀏覽此JSON?
var data ={
"first thing" :["1","2"],
"second thing" :["5","7"],
"third thing" :["8","2"]
};
我知道,我可以訪問像這樣的項目返回:
console.log(data["first thing"]);
但是,我需要通過他們迭代,並將它們應用到一些html,並且對象名稱會發生變化,所以我如何才能讓循環系統地通過它們並根據需要進行應用?基本上我首先需要遍歷並應用名稱,然後我將再次循環並應用值對。
processResults:function(data){
$('.thisclass').each(function(){
$(this).html('put first json name here, so "first thing"')
//then continue to loop through and apply next object name
});
}
然後下一個循環我需要做這樣的事情:
processResults:function(data){
$('.nextclass').each(function(){
$(this).html('put first of two values here, so "1"')
//then continue to loop through and apply next object value
});
}
問題是我不知道該怎麼做,而不在括號標記指定對象的名字!
['for for ... in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)或['Object.keys( )'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) – Pointy
https://developer.mozilla.org/en-US/docs/Web /JavaScript/Reference/Statements/for...in –