In my JSON array, the store
contains data for different roles and names.
[{
"role": "Executive Director",
"name": "David Z",
...},
{
"role": "Executive Director",
"name": "David Z",
...},
{
"role": "Non Executive Chairman",
"name": "Hersh M",
...},
{
"role": "Non Executive Director",
"name": "Alex C",
...},
{
"role": "Company Secretary",
"name": "Norman G",
...}]
Using this array, I generate multiple HTML tables dynamically.
As part of an AJAX success function, I iterate through the store
to create an HTML table like below:
var table = '';
table += '<tr><td......</td>';
$.each(store, function(i, data) {
// draw row...
// draw more rows...
});
table += '</tr></tbody>';
$("#table_d").append(table);
However, for one of the tables, I need to skip duplicate entries such as the second occurrence of David Z
.
var table = '';
table += '<tr><td......</td>';
$.each(store, function(i, data) {
if (i > 0 && store[i].name != store[i-1].name) {
// draw row...
// draw more rows...
}
});
table += '</tr></tbody>';
$("#table_d").append(table);
The array is always ordered so I can compare store[i].name
against store[i-1].name
to identify duplicate names.
What is the correct way to execute the loop when store[i].name != store[i-1].name
?