1

I am currently making a tiny script that tags cordenades in an array:

var paises = ['spain','australia'];
var leftX   = ['270','575'];
var topY    = ['60','220'];

When I need to access them:

clase = 'spain';
var left_ = leftX[paises.indexOf(clase)];
var top_ = topY[paises.indexOf(clase)];

This might be confusing for my workmates to update. How could I make it easier?

1
  • 2
    You got three identical answers in about 1 minute. I think your path forward is pretty clear :). Commented Nov 8, 2011 at 17:57

3 Answers 3

4
var places = {
    "spain":  {
         leftx: '270',
         topY: '60'
    },
    "australia":  {
         leftx: '575',
         topY: '220'
    }
}

//Then:
clase = 'spain';
var left_ = places[clase].leftX;
var top_ = places[clase].topY;
Sign up to request clarification or add additional context in comments.

Comments

3

I'd do it in an object structure:

var paises = {
    spain: { leftX: 270, topY: 60 },
    // etc
}

Then it's much easier to access, and you don't have to worry about .indexOf() support (this isn't available in older IE versions):

clase = 'spain';
var left_ = paises[clase].leftX;
var top_ = paises[clase].topY;

Comments

3

I would use javascript objects

var paises =  { spain: {leftX: 270, topY: 60}, australia: { leftX: 575, topY: 220};

paises.spain.leftX; // 270
// or
paises["spain"].leftX; // 270
// or
paises['spain']['leftX']; // 270

So there are many options to access the data, in a conventional JavaScript manor.

Comments

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.