如何有效创建多维javascript数组? [英] How do I efficiently create multi-dimensional javascript array?

查看:48
本文介绍了如何有效创建多维javascript数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储与此类似的信息:

I need to store information similar to this:

tag_array['design1']['0'][0]['x'] = 100;
tag_array['design1']['0'][0]['y'] = 100;
tag_array['design1']['0'][0]['w'] = 710;
tag_array['design1']['0'][0]['h'] = 332;

如果它是PHP,但不能在javascript中使用,则可以使用.

This would work if it is PHP but it's not working in javascript.

如何使用正确的javascript语法创建相同的数组?

How do I create the same array in proper javascript syntax?

推荐答案

在PHP中,您可以这样做,因为它也会成为父元素(实际上会引发警告).在JavaScript中,它会出错,因为父元素不存在.

In PHP, you can do that because it will make the parent elements too (it actually throws a warning). In JavaScript, it will error out because the parent elements don't exist.

与其像那样制作,不如一次将其制作成这样:

Instead of making it like that, try to make it all at once like this:

var tag_array = {
    design1: {
        '0': [{
            x: 100,
            y: 100,
            w: 710,
            h: 332
        }]
    }
};

这应该为您提供所需的结构.

This should give you the structure you want.

更新:答案已更新,以反映第一个 0 可以是字符串的事实.

UPDATE: Answer has been updated to reflect the fact that the 1st 0 can be a string.

这篇关于如何有效创建多维javascript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆