1

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?

1
  • 1
    what is the problem with doing var element = _.find(ppl, function (desc) { if (desc.name === text) return desc; }); console.log(element.content); Commented Sep 28, 2013 at 8:00

4 Answers 4

1

If I were you I would simply do a loop:

var length = ppl.length;
var findcat = function(){
   for (var a = 0; a < length; a++) { if(ppl[a].name==text){return ppl[a].content} };
}
var content = findcat();

rather than using underscore.js .

Or if you really want to use underscore.js, change it to this:

 var content = _.find(ppl, function (desc) { if (desc.name === text) return desc; });
 content = content.content;

and it will work.

Updates (regarding HTML strings in json):

It is okay to store them in json as these HTML strings will simply be considered as normal strings data (just don't forget to escape characters like quotation and forward slash). When real HTML elements are being created from these strings (using jquery functions like .html(string), append(string) ), the browser will need to render these new contents and it may cause a slow performance comparing to leaving all the page-rendering at the start for the browser, but the difference will be pretty subtle. So in terms of performance, it is always okay to have them in json. But in terms of security, you should be careful when there were HTML markup in your data because you are making XSS easier to be accomplished. (Here is a wikipedia article that provides more details on XSS, also known as Cross-site scripting.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. One question related my json object, Is it good to remove html tags from content: <p>description</p> or should I keep it? I am asking this because I want to know whether removing html elements from json is according to best practice or not. I need efficient solution and secure too.
About escape characters, should escape it at the backend or front end? If front end then how can I escape in my example above?
0

I don't think you need an array here. A simpler and more efficient way would be to use names as properties.

var ppl = {"John": "<p>description</p>", "Mike": "<p>Desc</p>" };

 $('a.ppl').on('click', function (e) {
        e.preventDefault();

        var text = $(this).text();

        console.log(ppl[text]);
 });

Comments

0

This is the expected Behavior of find operator which returns whole found item ! , why dont use content.content

Comments

0

the _.find looks through each value in the list, returning the first one that passes a truth test, when you return desc.content, it is evalued to true, so the desc object is return. so you can't return inside the find. but you can just access the content as desc.content. here is jsfiddle code:

 $('a.ppl').on('click', function (e) {
    e.preventDefault();

    var text = $(this).text();
    var desc = _.find(ppl, function (desc) { 
        return desc.name === text; 
    });
    console.log(desc.content);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.