Reputation: 14270
I am trying to show the record returned from my DB
my returned data is like the following:
UserID Username Fullname ID
141 john jerry1 170
141 john jerry1 18
141 john jerry1 1169
110 ted jerrytest 1701
110 ted jerrytest 18
I want to show all the ID in the page but avoid the duplicated username
so it will be like
john 170
18
1169
ted 1701
18
//I only want 2 divs (for john and ted) instead of 5.
My codes currently show
john 170 //1 loop
john 18 //1 loop
john 1169 //1 loop
ted 1701 //1 loop
ted 18 //1 loop
My codes:
for(var i=0; i<results.length; i++){
//create a div for each row...in my case, 5 divs/rows
//i tried to create a var a
a=i-1;
if(results[a]){
if(results[i].Username==results[a].Username){
continue;
}
}
//this will eliminate the duplicated john and ted but only show john 170 and ted 1701.
}
To recap, I want to show all the unique id in each row but not the duplcated username. Are there better way to do this? Thanks a lot!
Upvotes: 0
Views: 70
Reputation: 310
Why not combine both methods?
var endResult = "",
ids = [],
userName;
for (var i = 0, lt = results.length; i < lt; i++)
{
ids.push(results[i].ID);
userName = (i === 0 || results[i].Username != results[i-1].Username) ? results[i].Username : false;
if (userName)
{
endResult += "<div>" + userName + "</div>";
endresult += "<div>" + ids.join("<br>") + "</div>"
//Reset the list of ids
ids = [];
}
}
Upvotes: 2