2
Your iterations will never yield 1 name at a time since your $.each
is iterating over all of the contacts.
You should move your generateCard
function within your contacts iteration and use the obj
variable instead of passing the entire array (passing the entire array will either output undefined or [object Object]) into your generateCard
function.
$.each(contacts, function(idx, obj) {
var icon = "fa facogs";
$("table tr:last").append(generateCard(obj, icon));
});
Complete but contrived example and assuming a probably faulty json structure.
var contacts = [
{name: 'henrik'},
{name: 'sanaz'},
{name: 'esther'},
];
var generateCard = function (obj, icon) {
return "<tr><td>"+obj.name+'</td></tr>';
};
$("table").append("<tr></tr>");
$.each(contacts, function(idx, obj) {
var icon = "fa facogs";
$("table tr:last").append(generateCard(obj, icon));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table></table>
Update:
Your problem lies in your generateCard
function, change contactNameC
to contactNameC.name
.
Source:stackexchange.com