현재 두 개의 HTML 섹션 인 '표준'과 '추천'이 있습니다. 내 jQuery 코드는 JSON 객체를 두 범주로 나눠서 '추천'인지 여부에 따라 지정된 섹션 내에 표시합니다. 그러나 현재 '추천'값을 가진 모든 객체가 표시됩니다. 값이 'featured'인 마지막 4 개의 JSON 객체 만 섹션에 표시되도록 어떻게 변경합니까?마지막 4 개의 JSON 객체 만 표시
if
문을 추가하는 것만 큼 간단하지만 지금까지의 시도는 잘 진행되지 않았습니다.
{"blog":[
{
"title":"This is the title",
"content":"And this is the content",
"date":"01/12/11",
"featured":"Yes"
},
{
"title":"This is another title",
"content":"And this is some more content",
"date": "01/01/12",
"featured":"No"
},
{
// Continuation
}
]}
현재 jQuery를은 다음과 같습니다 :
$.getJSON("/TEST/readdb.php", function(json){
if(json.blogs.length > 0) {
$.each(json.blogs, function(){
var info = '<h2>' + this['title'] + '</h2><p>' + this['content'] + '</p><p id="date">' + this['date'] + '</p>';
if(this['featured'] == "Yes") {
$('#featured').append(info);
}
else {$('#standard').append(info);
}
});
}
});
비슷한 질문보기 http://stackoverflow.com/questions/777455/is-there-a-query-language-for-json – Daveo
가장 좋은 방법은 db에서 마지막 네 개의 추천 항목 만 가져 오는 것입니다. readdb.php에. – Diode
@Diode 당신은 절대적으로 옳다. 나는이 옵션을 결국 훨씬 더 간단하게 만들었습니다. – Ryan