我试图构建一个颜色结构, 每个项有 3 个数据 。 例如, 红色有 x 和 y, 蓝色有 x 和 y 等 。 因此, 3 个数据是 < code> color, x, y code > 。
需要什么样的结构来方便地根据颜色读取 x 和 y 。 我通常做 < code> push (color, x, y) code >, 但这里无法工作, 因为我需要用颜色快速搜索, 而不需要循环。 我在这里需要什么样的结构, 以及如何设置和获取它 。
我试图构建一个颜色结构, 每个项有 3 个数据 。 例如, 红色有 x 和 y, 蓝色有 x 和 y 等 。 因此, 3 个数据是 < code> color, x, y code > 。
需要什么样的结构来方便地根据颜色读取 x 和 y 。 我通常做 < code> push (color, x, y) code >, 但这里无法工作, 因为我需要用颜色快速搜索, 而不需要循环。 我在这里需要什么样的结构, 以及如何设置和获取它 。
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});
或者,如果您有像这样的结构
var colors = [
[ red , 837, 98],
[ blue , 25, 144],
[ yellow , 50, 12]
];
时当时
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 f或this structure
colors.push([ pink , 150, 200])
或
var colors = {
blue: { x: 58, y: 100 },
red: { x: 43, y: 1334 },
yellow: {x: 254, y: 12 }
}
时当时
for(var i in colors) {
console.log(colors[i].blue.x);
console.log(colors[i].blue.y);
// or
console.log(colors[i][ blue ].x);
// 或like
console.log(colors[i][ blue ][ x ]);
}
// and to insert f或this sturcture
colors.pink= {x: 150, y: 200};
简单对象( 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 ]);
还是我理解你错了?
你在找像字典一样的东西吗?
var colorArray = {};
colorArray["red"] = {
x: 100,
y: 200
};
colorArray["blue"] = {
x: 222,
y: 200
};
alert(colorArray["red"].x);
var colors = {
red : { x : 42, y : 7 },
blue : { x : .., y : .. },
...
};
alert(colors.red.x);
或者如果在数组中也需要颜色
var colors = {
blue: { color:"blue", x: 100, y: 200 },
red: { color:"red", x: 50, y: 300 },
yellow: { color:"yellow", x: 30 y: 700 }
}
您也可以使用字符串“ contents” :
var RED = "red";
var colors = {};
colors[RED] = { color: RED, x: 100, y: 200 };
...
How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.
I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...
Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...
Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...
I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.