Saturday, 28 September 2013

underscore find method issue on array of objects

underscore find method issue on array of objects

I have following array of objects
var ppl = [
{
name: "John",
content: "<p>description</p>"
},
{
name: "Mike",
content: "<p>Desc</p>"
},
{
name: "Steve",
content: "html"
},
{
name: "Michael",
content: "<p>description</p>"
}
];
What I am doing is to display above array. Then when user clicks on name
return his content. Like following
$('a.ppl').on('click', function (e) {
e.preventDefault();
var text = $(this).text();
var content = _.find(ppl, function (desc) { if (desc.name ===
text) return desc.content; });
console.log(content);
});
What above code does is it finds the content of the person clicked however
it returns the entire object of that person e.g. when John is clicked the
his entire object { name: "John", content: "<p>description</p>" } is
returned by the _.find() function. I just need the content. How can I
return content only?

No comments:

Post a Comment