Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么javascript数组、嵌套数组、对象最适合搜索_Javascript_Jquery - Fatal编程技术网

什么javascript数组、嵌套数组、对象最适合搜索

什么javascript数组、嵌套数组、对象最适合搜索,javascript,jquery,Javascript,Jquery,我试图建立一个颜色结构,每个项目有3个数据。例如,红色有x和y,蓝色有x和y,等等,所以3个数据是颜色,x,y 我需要什么样的结构才能根据颜色轻松读取x和y。我通常使用pushcolor,x,y,但在这里不起作用,因为我需要快速搜索颜色,而不需要循环。我在这里需要什么样的结构,以及如何设置和获取它 简单的对象散列怎么样 // Initial creation var colors = { blue: { x: 897, y: 98 }, red: { x: 43, y: 1334 },

我试图建立一个颜色结构,每个项目有3个数据。例如,红色有x和y,蓝色有x和y,等等,所以3个数据是颜色,x,y


我需要什么样的结构才能根据颜色轻松读取x和y。我通常使用pushcolor,x,y,但在这里不起作用,因为我需要快速搜索颜色,而不需要循环。我在这里需要什么样的结构,以及如何设置和获取它

简单的对象散列怎么样

// 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']);

还是我理解错了?

简单的对象散列怎么样

// 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']);
var colors = {
    red  : { x : 42, y : 7 },
    blue : { x : .., y : .. },
    ...
};

alert(colors.red.x);

还是我理解错了?

你在找字典之类的东西吗

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

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

你在找字典之类的东西吗

var colorArray = {};
colorArray["red"] = {
    x: 100,
    y: 200
};
colorArray["blue"] = {
    x: 222,
    y: 200
};
alert(colorArray["red"].x);​
或者如果你有这样的结构

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

然后

或者如果你有这样的结构

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

然后


或者,如果阵列中也需要颜色

var colors = {
 blue: { color:"blue", x: 100, y: 200 },
 red: { color:"red", x: 50, y: 300 },
 yellow: { color:"yellow", x: 30 y: 700 }
}
您还可以使用字符串常量:

var RED = "red";

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

或者,如果阵列中也需要颜色

var colors = {
 blue: { color:"blue", x: 100, y: 200 },
 red: { color:"red", x: 50, y: 300 },
 yellow: { color:"yellow", x: 30 y: 700 }
}
您还可以使用字符串常量:

var RED = "red";

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

如果你举个例子会更好