mardi 27 janvier 2015

Count instances of specific item w/Javascript

I'm working on pulling a list from sharepoint using the SPServices. The data loads fine, however I am trying to count the number of instances where a specific word/option is used. The data is something like:



"Name" : "George" , "Payment" : "CC"
"Name" : "Phillip", "Payment" : "CC"
"Name" : "Frances", "Payment" : "PO"
"Name" : "George", "Payment" : "PO"
"Name" : "George", "Payment" : "CC"


What I am trying to do is to count the number of instances where CC vs PO occurs for George. I've tried a couple things the only thing that kinda works - though it only displays '1' is:



for ( var i = 0; i< entries; i++) {
if (Name == "George") {
if (Payment == "PO") {
++POs;
}
if (Payment == "CC") {
++VCCs;
}
}
}


This however returns: George 1 George 1 George 1


EDIT: Per request here is the display:



$('myTable").append(
"<tr>" + "<td>" + Name + "</td>" +
"<td>" + Pos + "</td>" +
"<td>" + CCs + "</td>" +
"</tr>"
)

Aucun commentaire:

Enregistrer un commentaire