我的 Controller 中有一个创建搜索过滤器的功能。
搜索参数是从我的 Twig 模板的选择字段中获得的。
选定的选项被传递给 Controller ,以查看具有该值的结果。
查询结果以JSON格式返回。
public function categoryAction(Request $request){
$em = $this->getDoctrine()->getManager();
$category = $request->request->get('category');
$contentCategory = $em->getRepository('PublicartelAppBundle:Content')->findByCategory($category);
$filterContent = new JsonResponse();
$filterContent->setData([
'category_result' => $contentCategory
]);
return $filterContent;
}
$('#selectCategory').on('change', function () {
var optionSelect = $(this).val();
$.post("{{path('playlist_category') }}", {category: optionSelect},
function (filterContent) {
var result = filterContent.category_result;
console.log(result);
}, 'json');
});
控制台返回:
[Object, Object, Object]
O: Object
1: Object
2: Object
lenght: 3
返回的项目数量取决于找到的属于该类别的项目数量。
如果我执行以下操作,可以迭代每个元素。
for (var content in filterContent.category_result){
console.log(content, filterContent.category_result[content]);
}
然后在控制台中返回:
0 Object {}
1 Object {}
2 Object {}
但我想访问每个对象的“名称”属性。
我怎么能说要访问对象名称属性,或者至少每个都一一迭代?
最佳答案
如果它是适当的属性,您可以记录 filterContent.category_result[content].name
。
关于javascript - 在javascript中迭代数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31417624/