我的控制器中有一个功能来制作搜索过滤器。
搜索参数是从我的树枝模板的选择字段中获取的。
将所选选项传递给控制器,以查看该值的结果。
查询结果以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');
});
在控制台中返回:
[对象,对象,对象]
O:对象
1:对象
2:对象
lenght:3
返回的商品数量取决于在该类别中找到的商品数量。
如果我这样做,可以迭代每个元素。
for (var content in filterContent.category_result){
console.log(content, filterContent.category_result[content]);
}
然后在控制台中返回:
0对象{}
1对象{}
2对象{}
但我想访问每个对象的'name'属性。
我怎么能想要访问对象名称属性,或者至少逐个迭代?
答案 0 :(得分:4)
如果filterContent.category_result[content].name
是相应的属性,则可以记录。
答案 1 :(得分:1)
试试这个,看看“名字”在哪里:
for (var content in filterContent.category_result) {
console.log(content);
for (var i in filterContent.category_result[content]) {
console.log(filterContent.category_result[content][i]);
}
}