I have a json array and a simple array. Each object in this json has three keys & another simple array with unique values.
var jsonArr=[{
amount: "2",
club: "xyz",
member: "tmm"
},
{
amount: "3",
club: "xyz",
member: "tsj"
},
{
amount: "4",
club: "xyz",
member: "tsj"
},
{
amount: "5",
club: "ama",
member: "vbr"
},
{
amount: "6",
club: "ama",
member: "vbr"
},
{
amount: "7",
club: "crm",
member: "vbr"
},
{
amount: "8",
club: "mic",
member: "wjr"
}]
simple Array of members
var smpMember =['tmm','tsj','vbr','wjr']
simple Array of clubs
var smpClub=['xyz','ama','crm','mic']
I intend to create a simple array of amount value for each of the member.If the member has subscribe to a club it will add its amount value, if not it will assign a zero to it. For example member tmm has subscribe to club xyz so the array for it will be like
var tmm =[2,0,0,0];
Again member vbr has subscribe to club ama twice. So array for it will be
var vbr =[0,11,0,0]; // 11 is sum of 6 & 5
So there will be array for each of the member
What I tried?
I am looping taking each of the member from smpMember array and checking if that member exist in each of the json object.
for(var i=0,j=smapMember.length;i<j;i++){ // Loop through array of members & pick each member
for(var x=0,y=jsonArr.length;x<y;x++){ // Loop through jsaon array of objects
var _sAmt =0;
if(smapMember[i]==jsonArr[x].member){ // check if member exist
_sAmt =jsonArr[x].amount;
if(y > (x+1)){ // Check for last element
if(jsonArr[x].club ==jsonArr[x+1].club){
_sAmt +=jsonArr[x+1].amount;
}
else{
_sAmt =0
}
}
}
var tempArr.push(_sAmt);
}
But I am not able to sucessfuly get the result. Also I feel using too much if -else is an overhead. Can I have a better solution of it.
clubs['ama'].memberships[0].member.namegives you the name of the first member at the club ama. I can elaborate later when I have more time.