我正在尝试将下面的JSON输出到HTML。我已经创建了该对象以及该对象的属性,但是,我很难将其输出到HTML。这是我到目前为止所拥有的。有什么想法吗?
<p id="output-people"></p>
// Rest of the imports...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
/**
* @Route("/comment/add", name="comment_add")
* @Method({"POST"})
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addCommentAction(Request $request)
{
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isValid()) {
$comment->setDateCreated(new \DateTime());
$em = $this->getDoctrine()->getManager();
$topicId = $request->request->get('topicId');
$topic = $em->getRepository('Bundle:Topic')->find($topicId); // retrieve the topic id from the corresponding repository of the topic entity
$comment->setTopic($topic); // setting the topic to the comment
$em->persist($comment);
$em->flush();
$this->addFlash(
'notice',
'Comment Added Successfully !'
);
}
return $this->render('comments/comment.add.html.twig', array(
'commentsForm' => $form->createView()
));
}
You need also to add a repository from which you will retrieve the Topic objects. You need just an empty class named TopicRepository that should extend EntityRepository from Doctrine.
谢谢,
答案 0 :(得分:6)
这不是JSON.parse()
对象。正常数组Object.so删除myObj[0].first_name
。并更改输出innerHTML dom,如myObj.map(a=> a.first_name).sort().join(" and ")
<强>更新强>
对于所有first_name with sort .try this var people;
people = [{
first_name: 'Don Molloy',
category: 'academic'
},
{
first_name: 'Pat Quill',
category: 'academic'
}
];
people = people.sort((a,b)=> {
var a1 = a.first_name.toLowerCase();
var b1 = b.first_name.toLowerCase();
return a1<b1 ?-1:a1> b1? 1 :0;
})
console.log(people);
var myObj =people;
document.getElementById("output-people").innerHTML = myObj.map(a=> a.first_name).join(" and ")
<p id="output-people"></p>
&#13;
{{1}}&#13;
答案 1 :(得分:1)
As of your question "JSON Sorting by Alphabetical Order" you can first use Array.prototype.sort() to sort the array and than populate the output
variable and also use the separator
the way you want to show the names in the html.
Code:
var output = '',
separator = '<br>',
people = [{first_name: 'Don Molloy',category: 'academic'}, {first_name: 'Pat Quill',category: 'academic'}];
// Sort array of objects by first_name and populate the output
people
.sort(function (a, b) {
// ignore upper and lowercase
var nameA = a.first_name.toUpperCase(),
nameB = b.first_name.toUpperCase();
return nameA < nameB
? -1
: nameA > nameB
? 1
: 0;
})
.forEach(function (el) {
output += el.first_name + separator;
});
console.log(people);
document.getElementById('output-people').innerHTML = output;
<p id="output-people"></p>
Check how to properly sort an array of objects by a property string:
var arr = [{first_name: 'a'}, {first_name: 'c'}, {first_name: 'b'}];
arr.sort();
console.log(arr);
arr.sort(function (a, b) {
// ignore upper and lowercase
var nameA = a.first_name.toUpperCase(),
nameB = b.first_name.toUpperCase();
return nameA < nameB
? -1
: nameA > nameB
? 1
: 0;
});
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:0)
JS 中按字母顺序排序的 JSON =>
你可以试试这个:
function compareStrings(a, b) {
// Assuming you want case-insensitive comparison
a = a.toLowerCase();
b = b.toLowerCase();
return (a < b) ? -1 : (a > b) ? 1 : 0;
}
places.sort(function(a, b) {
return compareStrings(a.first_name, b.first_name);
})