简体   繁体   English

javascript:具有未知长度和未知索引名称的数组

[英]javascript: array with unknown length and unknown index names

I am making a program of a football league. 我正在制作一个足球联赛的程序。

A example of a league: 联赛示例:

team1 team2 team3 team1 team2 team3

I want to save (increase) number of players for each team like 我想为每个球队节省(增加)球员数量

var teamArray = [];
teamArray[team1] = 1;
teamArray[team2] = 2;

and I am doing in the body of some functions like: 我正在执行一些功能,例如:

function 1(){}
function 2(){}
function .(){}
function .(){}
function .(){}
function n(){}

but this works only when i "teach" javscript that the array is an integer array... with somethin like 但这仅在我“教” javscript该数组是整数数组时才有效……

teamArray[team1] = 0;
teamArray[team1] = teamArray[team1] + 1;

but the problem is every time when i come to one of my functions, i have to set my array element to 0 and that makes the whole calculation wrong... 但问题是每次我使用其中一个函数时,我都必须将数组元素设置为0,这会使整个计算出错...

Could anyone give me a hint please? 有人可以给我一个提示吗?

My Idea was to was to set each array element to 0 from the beginning, but i dont know at the beginning of my game how many teams i will have today, that why i implemented the array like: 我的想法是要从一开始就将每个数组元素设置为0,但是我不知道在我的游戏开始时我今天将拥有多少支球队,所以我为什么要像这样实现数组:

var teamArray = [];

Use a JavaScript object instead. 请改用JavaScript对象。 Something like: 就像是:

var teamObject = {
   team1: 3,
   team2: 5,
   team3: 7,
};

Or, perhaps an array of objects: 或者,也许是一组对象:

var teamArray = [
   { name: 'team1', players: 3 },
   { name: 'team2', players: 5 },
   { name: 'team3', players: 7 }
];

The object is easier to access if all you want is to get or set the number of players: 如果您想要获得或设置玩家人数,则更易于访问该对象:

teamObject.team1 += 1;

but an array is easier to loop through and can be ordered: 但是数组更容易遍历并且可以排序:

for (var i=0,j=teamArray.length; i<j; i++) {
  console.log(teamArray[i].name + " has " + teamArray[i].players + " players");
}

You can increment the number of team members by testing the current number first, and if it does not exist, you initialise it with 0 on the fly. 您可以通过首先测试当前数字来增加团队成员的数量,如果不存在,则可以动态地使用0对其进行初始化。 All that can be done in one expression with a logical OR ( || ): 使用逻辑或( || )在一个表达式中可以完成所有操作:

 teamArray[team1] = (teamArray[team1] || 0) + 1;

This will not destroy the previous value you had and work like a simple + 1 in that case. 在这种情况下,这不会破坏您以前的值,并且像简单的+ 1一样工作。

You should define your teamArray as object, although it will work with array as well (since that is an object as well): 您应该将teamArray定义为对象,尽管它也可以与array一起使用(因为它也是一个对象):

 teamArray = {}

The name is then of course a bit confusing, but I'll stick with it. 这样的名称当然有点令人困惑,但我会坚持下去。

Whenever you need to iterate over the teams you have collected, then you can use a for loop like this: 每当您需要遍历已收集的团队时,都可以使用如下所示的for循环:

for (var team in teamArray) {
    console.log(team, teamArray[team]);
}

thanks for all your help! 感谢你的帮助!

I did it like this now: 我现在是这样的:

var listItems = $("#teamsDropdown li");
    listItems.each(function(idx, li) {
        var team = $(li).text();
        TeamPlayerQuantities[team] = 0;             
    });

and increasing the qnty of players wiht functions like: 并通过以下功能增加玩家数量:

function1(team){
    TeamPlayerQuantities[team] ++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM