I have in my controller a function to make a search filter.
The search parameter is obtained from a select field of my twig template.
The selected option is passed to the controller to see where results are with that value.
The query result is returned in JSON format.
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');
});
This return in console:
[Object, Object, Object]
O: Object
1: Object
2: Object
lenght: 3
The amount of items returned depends on the number of items found with that category.
If I do the following could iterate each of the elements.
for (var content in filterContent.category_result){
console.log(content, filterContent.category_result[content]);
}
And return in console this:
0 Object {}
1 Object {}
2 Object {}
But I want to access the 'name' property of each object.
How could I say to want to access the object name property , or at least iterate one by one one each?
filterContent.category_result[content].name