배열을 반복하는 동안 문제가 있습니다. 즉, 자바 스크립트의 배열 내부에있는 객체 내부입니다. 아래는 내 루프이고 아래는 내 개체입니다. 개체의 이름을 검색하고 싶습니다. 내 $('#searchbox').keypress
기능과 내 var animals_data
물건을 비교하십시오
$('#searchbox').keypress(function (e) {
if (e.which == 13) {
var search_text = $('#searchbox').val();
console.log(search_text)
var filteredData = {
animalsR: animals_data.category.animalsR.filter(function(d){
if (d.name.search(search_text) > -1){
return true;
}
return false;
})
};
var source = $("#album-template-Reptile-result").html();
var template = Handlebars.compile(source);
var html = template(filteredData);
$('#content').html(html);
}
});
var animals_data = {
category : [{
name : "Reptiles",
animalsR : [
{
image1 : "url" ,
image2 : "url" ,
name : "Snake",
description : "text"
},
{
image1 : "url",
image2 : "url",
name : "Crocodilia",
description : "text"
}
]
}]
};
당신은을 통해 배열의 첫 번째 요소를 얻을 수 있습니다 [0]
, category
귀하의 경우는 인에Array
animals_data.category.animalsR.filter
// ^---- your error here, it's an array
반복 배열의 경우 다음을 사용할 수 있습니다. Array.prototype.forEach()
animals_data.category[0].animalsR.forEach(function(e){
// do something ...
})
그러나 배열 범주에 많은 객체가 있으면 어떻게 될까요? 각각은 내가 itterate하고 싶은 배열을 포함합니다.
이를 위해 다음 Array.forEach()
과 같이 중첩 된 메서드를 사용할 수 있습니다 .
animals_data.category.forEach(function(a) {
a.animalsR.forEach(function(e) {
// do something
});
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다