这只要工程确定它们仅仅是 一个条目被添加到数组中, 我添加了多个值,在其他的 单词中该函数将运行几个 时间然后我想发送整个数组的 ,但这似乎只是 将使用逗号 分隔符添加所有内容。
林不知道你在这里说什么。我之前给出的第二个示例假设每个uid
都有一个x,y对,但对_tags
中有多少个uid
没有限制。这就是为什么var _tags = {};
是功能的一部分 - 所以它是一个全局变量。
以下修改将允许你有多个X,Y对每个uid
:
function add_tag_queue(uid,x,y){
/*
* detect if _tags[uid] already exists with one or more values
* we assume if its not undefined then the value is an array...
* this is similar to doing isset($_tags[$uid]) in php
*/
if(typeof _tags[uid] == 'undefined'){
/*
* use an array literal by enclosing the value in [],
* this makes _tags[uid] and array with eah element of
* that array being a hash with an x and y value
*/
_tags[uid] = [{'x':x,'y':y}];
} else {
// if _tags[uid] is already defined push the new x,y onto it
_tags[uid].push({'x':x, 'y':y});
}
}
这应该工作:
function add_tag_queue(uid,x,y){
_tags.push([uid, x,y]);
}
如果你想uid
为重点那么你需要使用一个对象/散列不是array
var _tags = {}; // tags is an object
function add_tag_queue(uid,x,y){
_tags[uid] = {'x':x,'y':y};
}
这工作正常,只要他们只有一个条目被添加到数组中,我添加了多个值,换句话说该函数将运行几次,然后我想发送整个数组,但这似乎只是用逗号分隔符添加所有内容。 – Brian 2010-12-03 05:51:11