I have an svg map with several points where I want to store the initial position of each point in an array. And each point has it's own ID, like point_1, point_2 etc.
我有一个带有几个点的svg贴图,我想将每个点的初始位置存储在一个数组中。每个点都有自己的ID,如point_1,point_2等。
I have attached a click handler to each of these and run a function when they are clicked. So what I want to do in this function is to check if the array already contains the information of the clicked element. If it doesn't, I want to create it.
我已经为每个这些附加了一个点击处理程序,并在单击它们时运行一个函数。所以我想在这个函数中做的是检查数组是否已经包含被点击元素的信息。如果没有,我想创建它。
This is what I want to do, in pseudo code
这是我想要做的,在伪代码中
var arrPoints = [];
zoomToPoint('point_1');
function zoomToPoint(data_id) {
// Does array already contain the data?
if (!arrPoints.data_id) {
// Add data to the array
arrPoints.data_id.clientX = somevalue;
arrPoints.data_id.clientY = somevalue;
}
}
This would basically create an array that looks like this:
这基本上会创建一个如下所示的数组:
arrPoints.point_1[]
arrPoints.point_2[]
Where I can access the data in each .point_1 and .point_2. But I can't create an array based on a variable, like this:
我可以在哪里访问每个.point_1和.point_2中的数据。但我无法基于变量创建数组,如下所示:
arrPoints.data_id = [];
Because I end up with data_id as the actual name, not the variable that data_id actually is. So how is this usually accomplished? How can I identify each point to the actual array? Sorry for my lack of basics
因为我最终将data_id作为实际名称,而不是data_id实际上的变量。那通常如何实现呢?如何识别实际阵列的每个点?抱歉我缺乏基础知识
1 个解决方案
#1
2
Just use an object:
只需使用一个对象:
var arrPoints = {};
zoomToPoint('point_1');
function zoomToPoint(data_id) {
// Does array already contain the data?
if (!arrPoints[data_id]) { // square brackets to use `data_id` as index
// Add data to the array
arrPoints[data_id] = {};
arrPoints[data_id].clientX = somevalue;
arrPoints[data_id].clientY = somevalue;
}
}
#1
2
Just use an object:
只需使用一个对象:
var arrPoints = {};
zoomToPoint('point_1');
function zoomToPoint(data_id) {
// Does array already contain the data?
if (!arrPoints[data_id]) { // square brackets to use `data_id` as index
// Add data to the array
arrPoints[data_id] = {};
arrPoints[data_id].clientX = somevalue;
arrPoints[data_id].clientY = somevalue;
}
}