1

I have a javascript array of object that looks like this:

"partners":[{"username":"fangonk","profileImg":"fangonk.jpg"},
            {"username":"jane","profileImg":"jane.jpg"},
            {"username":"tom_jones","profileImg":"tom.jpg"}]

I would like to output the value of each array's key using the underscore library. So for each object, I'd like to output something that looks like this:

<h1>Username Value</h1><img src="profileImg Value here" />
5
  • 1
    Ooops - tired brain. Fixed. Commented Feb 28, 2013 at 17:17
  • 1
    What should the resulting array look like? Commented Feb 28, 2013 at 17:18
  • 1
    With a .map() function, typically you'd have the callback return a value and then store the return value of the .map() call, which will be a new Array of whatever your function returned. Commented Feb 28, 2013 at 17:18
  • That's not how you print values from an array or an object. Commented Feb 28, 2013 at 17:21
  • 1
    Ok, I can see that I was going about outputting the values in a weird way. I've restructured my question. Commented Feb 28, 2013 at 17:24

2 Answers 2

1

A bit confused about your "source", but I think you're just trying to do this:

_.each(partners, function(p) { document.write('<h1>' + p.username + '</h1>\
  <img src="' + p.profileImg + '" alt="' + p.username + '" />'); }
//substitute some DOM method (e.g. jquery().append) for document.write

Is this the result you're looking for?

this would only be applicable if your source looked more like:

var partners = [{"username":"fangonk","profileImg":"fangonk.jpg"},
                {"username":"jane","profileImg":"jane.jpg"},
                {"username":"tom_jones","profileImg":"tom.jpg"}];

EDIT:

var someBiggerObject = { 
  partners: [
    {"username":"fangonk","profileImg":"fangonk.jpg"},
    {"username":"jane","profileImg":"jane.jpg"},
    {"username":"tom_jones","profileImg":"tom.jpg"}
  ]
};

_.each(someBiggerObject.partners, function(p) { document.write('<h1>' + p.username + '</h1>\
  <img src="' + p.profileImg + '" alt="' + p.username + '" />'); }
//substitute some DOM method (e.g. jquery().append) for document.write
Sign up to request clarification or add additional context in comments.

1 Comment

The partners array is an attribute in a larger JSON object, so restructuring it could be difficult. Is there anyway to accomplish what I am looking for? Is there a way to restructure with underscore first?
0

The right way to do this is _.template

Example

If your structure is like this:

var list = {"partners":[ 
    {"username":"fangonk","profileImg":"fangonk.jpg"},
    {"username":"jane","profileImg":"jane.jpg"},
    {"username":"tom_jones","profileImg":"tom.jpg"}
]};

You can create the repeated item template (note type="text/html")

<script type="text/html" id="userItemTemplate">
    <h1><%= username %></h1><img src='<%= profileImg %>' />
</script>

and put each item into the template via a loop

var uIT = $("#userItemTemplate").html();
_.each(list.partners,function(user){
    $("#target").append(_.template(uIT,user));        
});

OR

put the loop into your template

<script type="text/html" id="userTemplate">
    <% _.each(partners,function(user,key,list){ %>
    <h1><%= user.username %></h1><img src='<%= user.profileImg %>' />
    <% }); %>
</script>

then push the whole array in

var uT = $("#userTemplate").html();
$("#target2").html(_.template(uT,list));

Note that I am using lodash instead of underscore. It's compatible for the most part, but I prefer lodash because the benchmarks are faster and the library is maintained well.

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.