繁体   English   中英

如何从函数内的数组调用项目

[英]How to call items from an array inside a function

我有一个函数“ zoom”,它采用以下格式:

zoom( [a,b,c,d....], [a,b,c,d...] );

我也有一个for循环,可以获取需要进入zoom数组的值:

ABC.getAggregation("V")[0].getItems().forEach( function (item) {
                  var a = item.getPosition().split(";")[0];
                  var b = item.getPosition().split(";")[1];
                  ABC.zoom( [...], [...] );
            });

如何将变量a和b添加到函数zoom的数组中?

所有变量a必须进入第一个数组,所有变量b必须进入第二个数组。

例:

 ABC.getAggregation("V")[0].getItems()
 //returns a list of 3 objects
 item.getPosition()
 //returns e.g "0,0,0" for the first item and so on (for all 3)
 item.getPosition().split(";")[0] = "0"
 //now i want to add this to the zoom function.

 var a = item.getPosition().split(";")[0]; 
//this produces three string values "14.5". "4", "8.64"


var b = item.getPosition().split(";")[1];
//this produces three string values "5.7","6.8","1"

现在,我想将这些字符串值放入缩放中,如下所示:

ABC.zoom( [14.5. 4, 8.64], [5.7,6.8,1] );
//note - they're not strings anymore.

我怎样才能达到这个结果?

Split返回一个字符串数组,因此item.getPosition().split(";")[0]; 将返回一个字符串,而不是三个字符串。 您需要使用,分隔符再次将其拆分,将结果数组解析为int(可以使用map函数)并传递给zoom函数。

您无法在.forEach()循环内执行对ABC.zoom()的调用,因为仅在执行所有迭代后才能获取整个数据集。

我认为您需要遵循以下原则:

var zoomA = [],
    zoomB = [];

ABC.getAggregation("V")[0].getItems().forEach( function (item) {
  var a = item.getPosition().split(";")[0];
  var b = item.getPosition().split(";")[1];
  zoomA.push(Number(a));
  zoomB.push(Number(b));
});

ABC.zoom(zoomA, zoomB);

如果我以某种方式误解了您要做什么,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM