0

我有 3 个数组

var city = [
  ['Kaunas', 54.896872,23.892426],
  ['Vilnius', 54.711136,25.280685],
  ['Klaipeda', 55.720149,21.131401],
  ['Utena', 55.536403,25.59494],
];

var lake = [
  ['Ezeras Bijote', 55.785092,23.062956],
  ['Ezeras Druksiai', 55.627996,26.565228],
  ['Ezeras Sartai', 55.804368,25.832863],
  ['Ezeras Metelys', 54.300299,23.767004],
];

var shop = [
  ['Kauno Akropolis', 54.891665,23.917744],
  ['Panorama', 54.709549,25.257454],
  ['Europa', 54.687514,25.262886],
  ['Ozas', 54.638628,25.135685],
];

我想将这 3 个数组添加到 1 个数组中,但不知道该怎么做,如果我可以像这样调用表单元素会非常好bigArr[city][1],,bigArr[shop][1]bigArr[lake][1]

4

4 回答 4

1

您可能对compact()感兴趣。

或者,只需将其声明为一个对象:

var myObject = {
    city: [
        ....
    ],
    lake: [
        ....
    ],
    shop: [
        ....
    ]
};
于 2012-07-27T16:51:31.700 回答
1

您可能想要创建一个对象而不是多维数组 -

var bigArr = {
    "city": {
        Kaunas: {
            "lat": 54.896872,
            "lon": 23.892426 
        },
        Vilnius: {
            "lat": 54.711136,
            "lon": 25.280685
        },
        Klaipeda: {
            "lat": 55.720149,
            "lon": 21.131401
        },
        Utena: {
            "lat": 55.536403,
            "lon": 25.59494
        }
    },

    "lake": {
        ...
    },

    "shop": {
        ...
    }
}

然后你可以使用它,比如bigArr.city.Kaunas.lat

于 2012-07-27T16:52:13.723 回答
1

使用您已有的:

var bigArr = {"city": city, "lake": lake, "shop": shop};

于 2012-07-27T16:53:32.347 回答
1

您应该使用对象而不是数组。您可以通过字符串键访问它们。将它们创建为文字:

var coordinates = {
    "city": {
        Kaunas:   [54.896872,23.892426],
        Vilnius:  [54.711136,25.280685],
        Klaipeda: [55.720149,21.131401],
        Utena:    [55.536403,25.59494]
    },
    "lake": {
        ...
    },
    "shop": {
        ...
    }
}

然后使用成员运算符访问它们的属性:

  • 点符号:coordinates.shop
  • 括号符号:coordinates["lake"]

要获取 Utena 的坐标数组,您可以使用coordinates.city["Utena"]

于 2012-07-27T16:59:32.173 回答