Javascript 声明包含对象的对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3581879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:17:39  来源:igfitidea点击:

Declare array of objects containing objects

javascriptarrays

提问by myforwik

I have an array of objects that contain other objects. At the moment the way I declare them is pretty long winded. Is there are more condense way to do the following:

我有一个包含其他对象的对象数组。目前,我声明它们的方式非常冗长。是否有更简洁的方法来执行以下操作:

function city(name,population)
{
 this.name = name;
 this.population = population;
}

function state(name)
{
 this.name= name;
 this.cities = new Array();
}

function country(name)
{
 this.name= name;
 this.states = new Array();
}

Countries = new Array();
Countries[0] = new Country("USA");
Countries[0].states[0] = new State("NH");
Countries[0].states[0].cities[0] = new city("Concord",12345);
Countries[0].states[0].cities[1] = new city("Foo", 456);
...
Countries[3].states[6].cities[35] = new city("blah", 345);

Is there a way to declare this setup that isn't so verbose, something similair to how an xml would be layed out, something like:

有没有办法声明这个设置不是那么冗长,类似于 xml 的布局方式,例如:

data =
usa
  NH
     concord: 34253
     foo: 3423
     blah: 99523
  NC
     place: 23522
Uk
  foo
     bar: 35929
     yah: 3452

I can't figure out how to nest array declarations without having to constantly repeat the variable names.

我无法弄清楚如何在不必不断重复变量名称的情况下嵌套数组声明。

回答by slebetman

Use object and array literals:

使用对象和数组文字:

var data = {
    usa : {
        NH : {
           concord: 34253,
           foo: 3423,
           blah: 99523
        },
        NC : {
           place: 23522
        }
    },
    uk : {
      foo : {
         bar: 35929,
         yah: 3452
      }
    }
}

Or something that reflects your original code more directly:

或者更直接地反映您的原始代码的内容:

var Countries = [ 
    {
        name : 'USA',
        states : [
            {
                name : 'NH',
                cities : [
                    {
                        name : 'Concord',
                        population : 12345
                    },
                    {
                        name : "Foo",
                        population : 456
                    }
                    /* etc .. */
                ]
            }
        ]
    },
    {
        name : 'UK',
        states : [ /* etc... */ ]
    }
]

Note: In javascript, var foo = []is exactly equivalent to (and the preferred way to write) var foo = new Array(). Also, var foo = {}is the same as var foo = new Object().

注意:在 javascript 中,var foo = []完全等同于(以及首选的编写方式)var foo = new Array()。此外,var foo = {}var foo = new Object().

Note: Don't forget to add commas between separate sub-objects.

注意:不要忘记在单独的子对象之间添加逗号。

回答by syockit

Would this be good enough for you?

这对你来说足够好吗?

var data = [
  { name: "USA",
    states: [
      { name: "NH",
        cities: [
          { name: "concord", population: 34253 },
          { name: "foo", population: 3423 },
          { name: "blah", population: 99523 }
        ]
      },
      { name: "NC",
        cities: [
          { name: "place", population: 23522 }
        ]
      }
    ]
  },
  {
    name: "UK",
    states: [
      { name: "foo",
        cities: [
                  { name: "bar", population: 35929 },
                  { name: "yah", population: 3452 }
                ]
      }
    ]
  }
]

回答by Scott Evernden

data = {
 usa: {
   NH: {
     concord: 34253,
     foo: 3423,
     blah: 99845
   }
 uk: {
   foo: {
     bar: 3454,
     yah: 33457
   }
  }
}

}

}