1

I'm trying to build a colors structure that has 3 data per item. For example, red has x and y, blue has x and y, etc. So the 3 pieces of data are color, x, y

What structure do I need to make it easy to read the x and y based on the color. I usually do push(color, x, y) but that wouldn't work here, because I need to search quickly by the color without needing to loop. What structure do I need here, and how do I set it and get it.

1
  • 1
    it would be better if you give example Commented May 27, 2012 at 7:36

5 Answers 5

4

What about a simple object (hash) ?

// Initial creation
var colors = {
  blue: { x: 897, y: 98 },
  red: { x: 43, y: 1334 },
  yellow: { y: 12 }
}

// Adding new element to existing object
colors['green'] = { x: 19 };

// Accessing them
console.log(colors.blue.x);
console.log(colors.yellow.y);

// Accessing them with name in var
var needed = 'green';
console.log(colors[needed].x);
console.log(colors[needed]['x']);

Or did I understand you wrong?

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

Comments

3

Are you looking for something like a dictionary?!?

var colorArray = {};
colorArray["red"] = {
    x: 100,
    y: 200
};
colorArray["blue"] = {
    x: 222,
    y: 200
};
alert(colorArray["red"].x);​

Comments

2
var colors = {
    red  : { x : 42, y : 7 },
    blue : { x : .., y : .. },
    ...
};

alert(colors.red.x);

Comments

2

Or if you need the color also in the array

var colors = {
 blue: { color:"blue", x: 100, y: 200 },
 red: { color:"red", x: 50, y: 300 },
 yellow: { color:"yellow", x: 30 y: 700 }
}

You also could use string "constants":

var RED = "red";

var colors = {};
 colors[RED] = { color: RED, x: 100, y: 200 };
 ...

Comments

1
var colors = [
  {color: 'blue',  x: 897, y: 98 },
  {color: 'red', x: 25,  y: 1334 },
  {color: 'yellow', x: 50, y: 12 }
]

for(var i in colors) {
  console.log(colors[i].color);
  console.log(colors[i].x);
  console.log(colors[i].y);
}
// To insert into colors

colors.push({color: 'pink', x: 150, y: 200});

or if you have structure like this

var colors = [
   ['red', 837, 98], 
   ['blue', 25, 144], 
   ['yellow', 50, 12]
];

then

for(var i in colors) {
  console.log(colors[i][0]); // output: red, yellow ...
  console.log(colors[i][1]); // output: 837, 25 ..
  console.log(colors[i][2]); // output: 98, 144 ..
}

and to insert into colors for this structure
colors.push(['pink', 150, 200])

or

var colors = {
  blue: { x: 58, y: 100 },
  red: { x: 43, y: 1334 },
  yellow: {x: 254, y: 12 }
}

then

for(var i in colors) {
  console.log(colors[i].blue.x);
  console.log(colors[i].blue.y);
  // or
  console.log(colors[i]['blue'].x);
  // or like
  console.log(colors[i]['blue']['x']);
}

// and to insert for this sturcture

colors.pink= {x: 150, y: 200};

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.