I have two arrays, one is an Array of Objects like this:
[
Object {ID: "2006", Description: "ABCDEFG"}
Object {ID: "2102", Description: "HIJKLMN"}
Object {ID: "2616", Description: "OPQRSTU"}
]
And the another is an array with the attributes
["ID", "Description"]
I'm trying to use JQuery .each function to capture the values using the Array as reference and create a HTML table, just like this:
var columns = new Array();
var strTable = '';
var tHead = '';
var tBody = '';
//Capture the columns
$.each(arrObjects, function (a, b) {
columns=Object.getOwnPropertyNames(b)
});
//Make the Table Head;
$.each(columns, function (a, b) {
tHead += '<th>'+ b +'</th>'
});
//Create table body
$.each(arrObjects, function (aa, bb) {
tBody += '<tr>'
$.each(columns, function (a, b) {
tBody += '<td>'+ bb.b +'</td>'
});
tBody += '</tr>'
})
strTable = '<table class="table"><thead><tr>' + tHead + '</tr></thead><tbody>' + tBody + '</tbody></table>'
But using this way, I am always getting the value undefined
.
Could you help me create a function that receive one Array of Objects and it retrieve a table? Or help me find out what I'm doing wrong it's ok too.