我有一个这样的数据数组:
var data = [
["Acid", 0.741593940836, 0.45657115],
["Cannabis", 0.94183423, 0.31475],
["LSD", 0.1367547, 0.936115]
];
哪个图指向散点图。
我还有其他类似的数据数组-在data
中用每个子数组的相同名称声明这些数组。然后将这些数组存储在一个数组中:
var Acid = ["leak","advice", "draft","addition","genuine","object","advance","freshman","sour","say","page","old","loot","white","soul","wriggle","agony","sensitivity","laundry","format"];
var collection = [Acid, Cannabis, LSD];
我正在尝试创建一些代码,根据散点图中的点选择返回一个(两个或全部)数组(Acid
,Cannabis
,LSD
) 。到目前为止,我的代码如下-应该注意的是,选择点是通过Lasso
完成的,我也包括了该代码。
var lasso_end = function() {
lasso.items()
.classed("not_possible",false)
.classed("possible",false);
var selected = lasso.selectedItems()
.classed("selected", true)
.attr("r", 13);
var selectedPoints = [];
selected.data().forEach((arr) => {
arr.forEach((d) => {
selectedPoints.push(d);
});
});
for(var i = 0; i < selectedPoints.length; i++) {
for(var j = 0; j < collection.length; j++) {
if(selectedPoints[0] == collection[j]) {
console.log(collection[j]);
}
}
}
只需重申一下,我正在尝试将数组Acid
,Cannabis
和LSD
的数据记录到控制台,如果指向数组data
被选中
针对其中一项评论,我在console.log()
之后放置了selectedPoints
,这是输出和格式:
答案 0 :(得分:1)
好吧,如果我理解正确,那么做完
selected.data().forEach((arr) => {
arr.forEach((d) => {
selectedPoints.push(d);
});
});
您的selectedPoints数组注销为
"Acid",
0.123123,
0.123131,
"Cannabis"
0.232222,
0.221121... etc.
,然后您要console.log名称在上面输出中的数组,这里是数组Acid
和Cannabis
。
问题在于,您创建的数组collection
的命名变量包含单词array:保存该数组的变量的名称无法传输,您不能执行
var foo = 2
var array = [foo]
并尝试使用array[foo]
访问该值,这不是JS数组的工作方式。他们只有数字索引。
您应该使用一个对象:
var collection = {
Acid: ["leak","advice", "draft","addition","genuine","object","advance","freshman","sour","say","page","old","loot","white","soul","wriggle","agony","sensitivity","laundry","format"]
}
,然后从selectedPoints
var pointNames = selectedPoints.filter(x => typeof x === "string")
并遍历pointNames数组,并在对象collection
上注销相应的属性(如果存在)。这是一个简化的代码段:
var data = [
["Acid", 0.741593940836, 0.45657115],
["Cannabis", 0.94183423, 0.31475],
["LSD", 0.1367547, 0.936115]
];
var selectedPoints = ['Acid', 0.741593940836, 0.45657115];
var collection = {
Acid: ["leak","advice", "draft","addition","genuine","object","advance","freshman","sour","say","page","old","loot","white","soul","wriggle","agony","sensitivity","laundry","format"]
}
var pointNames = selectedPoints.filter(x => typeof x === "string")
pointNames.forEach(point => {
if(collection[point]) {
console.log(collection[point])
}
})