简体   繁体   English

连接 Javascript 函数的参数名称

[英]Concatenate the Parameter Name of a Javascript Function

I have some working code which I'm trying to optimize using a loop but am having trouble concatenating the name of a parameter.我有一些工作代码,我正在尝试使用循环对其进行优化,但在连接参数名称时遇到问题。

 var objlocations = {}; $.get('https://example.com/something/ajax-state?somevar=' + somevar, function(data) { $.each(data, function(index,DBRow){ objlocations['point1x'] = DBRow.Point01X; objlocations['point1y'] = DBRow.Point01Y; objlocations['point2x'] = DBRow.Point02X; objlocations['point2y'] = DBRow.Point02Y; objlocations['point3x'] = DBRow.Point03X; objlocations['point3y'] = DBRow.Point03Y; objlocations['point4x'] = DBRow.Point04X; objlocations['point4y'] = DBRow.Point04Y; // There are about 30 more of these needed var $grid = jQuery('#MyDiv'); $grid.empty(); for(var i=1;i<=4;i++) { var x = objlocations['point' + i + 'x']; var y = objlocations['point' + i + 'y']; // doesnt' work: var x = 'DBRow.Point0'+i+'X'; // doesn't work: var y = 'DBRow.Point0'+i+'Y'; var $PointLocation = jQuery('<img class="PointLocation" id="Grid-Point-' + i + '"' + 'src="../img/points/point.png" width="50px">').css({top:y + 'px', left:x + 'px'}); $grid.append($PointLocation); } }); });

For the variables "x" and "y" I'd like to loop through and directly use DBRow.PointxX rather than first manually dumping them into an object since there will be over 60 lines required.对于变量“x”和“y”,我想循环并直接使用 DBRow.PointxX 而不是首先手动将它们转储到对象中,因为将需要 60 多行。

As per the comment in the code I've tried var x = 'DBRow.Point0'+i+'X' which results in "x" having "DBRow.Point01X" etc as a string value.根据代码中的注释,我试过 var x = 'DBRow.Point0'+i+'X' 这导致“x”具有“DBRow.Point01X”等作为字符串值。

If I try without the quotes like this: DBRow.Point0+i+XI get a console error stating that X is not defined.如果我尝试不使用这样的引号: DBRow.Point0+i+XI 得到一个控制台错误,指出 X 未定义。

I'll also have to do something about the leading 0 on the DBRow.Point##X but I may be able to do this in the database I'm pulling from.我还必须对 DBRow.Point##X 上的前导 0 做一些事情,但我可以在我从中提取的数据库中执行此操作。

I'm new to Javascript so hopefully this is something simple that I'm overlooking.我是 Javascript 新手,所以希望这是我忽略的简单内容。

Edit: Final solution编辑:最终解决方案

 $.get('https://example.com/something/ajax-state?somevar=' + somevar, function(data) { $.each(data, function(index,DBRow){ var $grid = jQuery('#MyDiv'); $grid.empty(); for(var i=1;i<=32;i++) { var ii = ('00'+i).slice(-2); var x = DBRow["Point"+ii+"X"]; var y = DBRow["Point"+ii+"Y"]; var $PointLocation = jQuery('<img class="PointLocation" id="Grid-Point-' + i + '"' + 'src="../img/points/point.png" width="50px">').css({top:y + 'px', left:x + 'px'}); $grid.append($PointLocation); } }); });

Moved comment to answer for more explanation.移动评论以回答更多解释。

Objects in javascript can be accessed like named array items. javascript 中的对象可以像命名数组项一样访问。

DBRow.Point01X is the same as DBRow['Point01X'] DBRow.Point01X 与 DBRow['Point01X'] 相同

You could make a loop where the name is prepared then inserted into the array.您可以创建一个循环,其中准备好名称然后插入到数组中。

Ex:前任:

i = (i < 10) ? ("0" + i) : i; // for leading zeros
var point = 'Point' + i + 'X';

objlocations['point'+i+'x'] = DBRow[point];
 for(var i=1;i<=4;i++) {
      var xProperty = "Point0" + i + 'X';
      var yProperty = "Point0" + i + 'Y';
      var x = DBRow[xProperty];
      var y = DBRow[yProperty];
      var $PointLocation = jQuery('<img class="PointLocation" id="Grid-Point-' + i + '"' + 'src="../img/points/point.png" width="50px">').css({top:y + 'px', left:x + 'px'});
      $grid.append($PointLocation); 
    }

Since the properties are variable and not a literal, so you will have to use the bracket notation instead of the dot notation .由于属性是可变的而不是文字,因此您必须使用括号表示法而不是点表示法

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

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