我如何动态创建一个多维javascript数组

时间:2022-03-27 21:26:33

I'm trying to dynamically build a javascript array where _tags is a globally defined array and then send it to php via ajax request. Basically I need the uid as a key and x,y as a sub array. in php it would look something like

我正在尝试动态构建一个javascript数组,其中_tags是一个全局定义的数组,然后通过ajax请求将其发送到php。基本上我需要uid作为键,x,y作为子数组。在PHP中它看起来像

$arr[$uid] = array('x'=>$x,'y'=>$y); 

but im having trouble figuring out an array like this in javascript, heres what i have

但我很难在javascript中找出这样的数组,这就是我所拥有的

function add_tag_queue(uid,x,y){

    var arr = new Array(3);
    arr[0] = uid;
    arr[1] = x;
    arr[2] = y;

    _tags.push(arr);
}

5 个解决方案

#1


4  

This works ok as long as their is only one entry being added to the array, I'm adding multiple values, in other words the function will run a few times and then i want to send that entire array, but this seems to just be adding everything with a comma delimeter.

只要它们只有一个条目被添加到数组中,这就可以了。我正在添加多个值,换句话说,该函数将运行几次然后我想发送整个数组,但这似乎只是用逗号分隔符添加所有内容。

Im not sure what youre saying exactly here. The second example i previously gave assumes there is a single x,y pair for each uid but places no limits on how many uids are in _tags. Thats why var _tags = {}; is ourside of the function - so its a global variable.

我不确定你到底在说什么。我之前给出的第二个例子假设每个uid都有一个x,y对,但对_tags中有多少uid没有限制。这就是为什么var _tags = {};是函数的我们 - 所以它是一个全局变量。

The following modifications would allow you to have multiple x,y pairs for each uid:

以下修改将允许您为每个uid具有多个x,y对:

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});
   }
}

This should work:

这应该工作:

function add_tag_queue(uid,x,y){

    _tags.push([uid, x,y]);
}

if you want the uid as the key then you need to use an object/hash not an array

如果你想将uid作为键,那么你需要使用object / hash而不是数组

var _tags = {}; // tags is an object
function add_tag_queue(uid,x,y){

        _tags[uid] = {'x':x,'y':y};
    }

#2


0  

You could always just build it in php and json_encode it.

你总是可以在php和json_encode中构建它。

#3


0  

You're looking to implement an associative array in Javascript. While Javascript does not support associative arrays, Javascript objects can be treated much the same.

您正在寻找在Javascript中实现关联数组。虽然Javascript不支持关联数组,但Javascript对象可以被大致相同。

Try this instead:

试试这个:

_tags = {}

function add_tag_queue(uid,x,y){
    _tags[uid] = {x:x, y:y};
}

_tags is now an object and you'll be adding a new object at the uid key. Likewise, the x,y pair is stored in an object. The first x is the key and the second is the value. To clarify, you could write it like this:

_tags现在是一个对象,你将在uid键上添加一个新对象。同样,x,y对存储在对象中。第一个x是键,第二个是值。为了澄清,你可以像这样写:

function add_tag_queue(uid,xValue,yValue){
    _tags[uid] = {x:xValue, y:yValue};
}

#4


0  

It looks very similar to the PHP example you gave:

它看起来非常类似于您给出的PHP示例:

function add_tag_queue(uid,x,y){
    _tags[uid] = {x: x, y: y};
}

#5


0  

This is how I create multidimensional arrays in JS. I hope this helps.

这就是我在JS中创建多维数组的方法。我希望这有帮助。

var arr= [];
arr[uid] = new Array();
arr[uid]["x"] = xvalue;
arr[uid]["y"] = yvalue;

#1


4  

This works ok as long as their is only one entry being added to the array, I'm adding multiple values, in other words the function will run a few times and then i want to send that entire array, but this seems to just be adding everything with a comma delimeter.

只要它们只有一个条目被添加到数组中,这就可以了。我正在添加多个值,换句话说,该函数将运行几次然后我想发送整个数组,但这似乎只是用逗号分隔符添加所有内容。

Im not sure what youre saying exactly here. The second example i previously gave assumes there is a single x,y pair for each uid but places no limits on how many uids are in _tags. Thats why var _tags = {}; is ourside of the function - so its a global variable.

我不确定你到底在说什么。我之前给出的第二个例子假设每个uid都有一个x,y对,但对_tags中有多少uid没有限制。这就是为什么var _tags = {};是函数的我们 - 所以它是一个全局变量。

The following modifications would allow you to have multiple x,y pairs for each uid:

以下修改将允许您为每个uid具有多个x,y对:

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});
   }
}

This should work:

这应该工作:

function add_tag_queue(uid,x,y){

    _tags.push([uid, x,y]);
}

if you want the uid as the key then you need to use an object/hash not an array

如果你想将uid作为键,那么你需要使用object / hash而不是数组

var _tags = {}; // tags is an object
function add_tag_queue(uid,x,y){

        _tags[uid] = {'x':x,'y':y};
    }

#2


0  

You could always just build it in php and json_encode it.

你总是可以在php和json_encode中构建它。

#3


0  

You're looking to implement an associative array in Javascript. While Javascript does not support associative arrays, Javascript objects can be treated much the same.

您正在寻找在Javascript中实现关联数组。虽然Javascript不支持关联数组,但Javascript对象可以被大致相同。

Try this instead:

试试这个:

_tags = {}

function add_tag_queue(uid,x,y){
    _tags[uid] = {x:x, y:y};
}

_tags is now an object and you'll be adding a new object at the uid key. Likewise, the x,y pair is stored in an object. The first x is the key and the second is the value. To clarify, you could write it like this:

_tags现在是一个对象,你将在uid键上添加一个新对象。同样,x,y对存储在对象中。第一个x是键,第二个是值。为了澄清,你可以像这样写:

function add_tag_queue(uid,xValue,yValue){
    _tags[uid] = {x:xValue, y:yValue};
}

#4


0  

It looks very similar to the PHP example you gave:

它看起来非常类似于您给出的PHP示例:

function add_tag_queue(uid,x,y){
    _tags[uid] = {x: x, y: y};
}

#5


0  

This is how I create multidimensional arrays in JS. I hope this helps.

这就是我在JS中创建多维数组的方法。我希望这有帮助。

var arr= [];
arr[uid] = new Array();
arr[uid]["x"] = xvalue;
arr[uid]["y"] = yvalue;