0

I have 3 arrays

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],
];

I want add this 3 arrays to 1 array ut don't know how to do this, will very nice if i can call form alements like this bigArr[city][1], bigArr[shop][1],bigArr[lake][1]

4 Answers 4

1

Using what you already have:

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

Sign up to request clarification or add additional context in comments.

2 Comments

bigArr["city"][0] is the same as city[0]
You can also use bigArr.city[0]
1

You may be interested in compact().

Alternatively, just declare it as an object already:

var myObject = {
    city: [
        ....
    ],
    lake: [
        ....
    ],
    shop: [
        ....
    ]
};

Comments

1

You should use Objects instead of Arrays. You can access them by string keys. Create them as a literal:

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": {
        ...
    }
}

Then access their properties by using member operators:

  • Dot notation: coordinates.shop
  • Bracket notation: coordinates["lake"]

To get the coordinates array for Utena, you might use coordinates.city["Utena"]

Comments

1

You might want to create an object instead of a multidimensional array -

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": {
        ...
    }
}

And then you can use it, like bigArr.city.Kaunas.lat

1 Comment

Never store numbers as strings!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.