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

发布于 2024-10-05 18:17:52 字数 384 浏览 0 评论 0原文

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

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

,但我在 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);
}

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

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

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

function add_tag_queue(uid,x,y){

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

    _tags.push(arr);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

饮湿 2024-10-12 18:17:52

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

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

以下修改将允许您为每个 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});
   }
}

这应该可行:

function add_tag_queue(uid,x,y){

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

如果您希望将 uid 作为键,那么您需要使用一个对象/hash 不是数组

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

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

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.

The following modifications would allow you to have multiple x,y pairs for each 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});
   }
}

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

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

        _tags[uid] = {'x':x,'y':y};
    }
看海 2024-10-12 18:17:52

你总是可以用 php 构建它并对其进行 json_encode 。

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

聚集的泪 2024-10-12 18:17:52

您正在寻找在 Javascript 中实现关联数组。虽然 Javascript 不支持关联数组,但 Javascript 对象的处理方式大致相同。

请尝试这样做:

_tags = {}

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

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

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

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.

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:

function add_tag_queue(uid,xValue,yValue){
    _tags[uid] = {x:xValue, y:yValue};
}
夜吻♂芭芘 2024-10-12 18:17:52

它看起来与您给出的 PHP 示例非常相似:

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

It looks very similar to the PHP example you gave:

function add_tag_queue(uid,x,y){
    _tags[uid] = {x: x, y: y};
}
暖阳 2024-10-12 18:17:52

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

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

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

var arr= [];
arr[uid] = new Array();
arr[uid]["x"] = xvalue;
arr[uid]["y"] = yvalue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文