I'm trying to loop over an Array using Underscore, I have this:
var friends = [
{
name: 'Jimmy',
age: 21
},
{
name: 'Anna',
age: 19
},
{
name: 'Alina',
age: '22'
},
{
name: 'Carl',
age: '22'
}
];
var names = _(friends).pluck('name');
for(i = 0; i < friends.length; i++){
$('ul').append('<li>' + names[i] + '</li>');
}
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul></ul>
I'm trying to do the same using underscore's each method, but I can't achieve it, I'm trying doing something like this:
_(friends).each(function(namessss, i){
$('ul').append('<li>' + names[i] + '</li>');
});
This actually works, but what I don't understand is why?! What's the first parameter for, it doesn't matter what I write there, it works and I wonder how to do this, and why this way works, thanks.
console.log(namessss)It does not matter what you name it since you do not use it.