1

I have a list that is setup like this from a sql database

 Team     Name    Date Joined
 TeamA    David   June 3
 TeamB    Mike    July 2
 TeamC    Mark    June 7
 TeamA    Lisa    July 2
 TeamA    Stacy   June 7
 TeamB    Tracy   July 2
 TeamC    Joe     June 3

I want to be able to store the list and print all the people and dates joined in TeamA or TeamB or TeamC using javascript. I am not sure how to approach this, was thinking about arrays but don't think that won't reach my goal because not sure how to combine all the items together...does anyone have ideas

4

2 回答 2

1

如果你只想存储每个团队成员的名字,那么你可以使用这个:

var Teams = {
    TeamA: ["David", "Lisa", "Stacy"],
    TeamB: ["Mike", "Tracy"],
    TeamC: ["Mark", "Joe"]
};

如果要将这些值打印到浏览器控制台中

for (team in Teams){
    console.log(team+" : "+Teams[team].join(", "));
}
于 2013-09-18T03:36:07.110 回答
1

您可以将您的团队定义为对象数组:

var arr = [

 {"team":"TeamA", "name":"David"},
 {"team":"TeamB", "name":"Mike"},
 {"team":"TeamC", "name":"Mark"},
 {"team":"TeamA", "name":"Lisa"},
 {"team":"TeamA", "name":"Stacy"},
 {"team":"TeamB", "name":"Tracy"},
 {"team":"TeamC", "name":"Joe"}

]

它很灵活,您可以根据需要添加任意数量的团队/人员。然后你可以定义一个简单的过滤函数:

var teamFilter = function(obj) {
    return obj.team == this.criteria;
}

您可以调用它来创建新的过滤数组,如下所示:

arr.criteria = "TeamA";

var arrTeamA = arr.filter(teamFilter, arr);

演示:http: //jsfiddle.net/6f2NN/

于 2013-09-18T03:53:34.050 回答