简体   繁体   English

Javascript:根据多个变量名称编译字符串

[英]Javascript: Compile string based on multiple variable names

This should be bone simple, and as usual, I'm probably overlooking something due to my less-than-thorough way of teaching myself code. 这应该很简单,并且像往常一样,由于我自学代码的方式不够深入,我可能忽略了某些东西。 Hopefully this makes sense... 希望这是有道理的...

In a php loop, I have this: 在php循环中,我有这个:

var key = <?php echo json_encode($key); ?>;
var id<?php echo $key;?> = <?php echo json_encode($value['Claim']['id']); ?>;
var point<?php echo $key;?> = <?php echo json_encode($value['Claim']['point']); ?>;

Which evaluates just as I want it. 正如我想要的那样评估。 So id2 equals whatever id2 is supposed to be, etc. 所以id2等于id2应该是的,依此类推。

Then later I need to call those variables, like this: 然后,稍后我需要调用这些变量,如下所示:

var takeurl = '/api/response.json?point='+ point + '&id=' + id;

Except I want "point" and "id" to be the value of "point2" or "point4" or, in shorthand, point+key. 除了我希望“ point”和“ id”是“ point2”或“ point4”的值,或者简而言之,就是“ point + key”的值。

If I write it as 如果我写成

var takeurl = '/api/response.json?point='+ point + key + '&id=' + id;

it returns the value of "point" plus the value of "key," not the value of point2 or point3. 它返回“ point”的值加上“ key”的值,而不是point2或point3的值。

Then I realise that at the very beginning, when declaring those values, I shouldn't have to hit php all the time. 然后我意识到,一开始声明这些值时,我不必一直打php。 I should be able to do something like: 我应该能够做类似的事情:

var key = <?php echo json_encode($key); ?>;
var id[key] = <?php echo json_encode($value['Claim']['id']); ?>;
var point{"key"} = <?php echo json_encode($value['Claim']['point']); ?>;

And then create the new variable using the same convention. 然后使用相同的约定创建新变量。 I've read similar questions where the answer involves using arrays, but again, this is all in a larger php loop, so I don't see that fitting the situation here. 我已经读过类似的问题,其中的答案涉及使用数组,但是同样,这都在一个较大的php循环中,所以我在这里看不到适合的情况。

Sorry, I hope this was clear. 抱歉,我希望这很清楚。 I'm earning my username on this one. 我正在此帐户上获得用户名。 Ideas? 有想法吗?

EDITED TO ADD: The problem is a little different than what I've explained above. 编辑添加:该问题与我上面解释的有所不同。 My code is apparently pulling the values of the final variables that appear in the loop (as if I weren't defining them by a 'key' distinction at all, but just defining "point" five times). 我的代码显然正在提取出现在循环中的最终变量的值(好像我根本不是通过“键”的区别来定义它们,而只是定义了“ point”五次)。 I fear I'm not making sense at all. 我担心我根本没有道理。 Here's some code so you can see what's going on: codepad.org/ETX5dI6C 这是一些代码,因此您可以查看发生了什么:codepad.org/ETX5dI6C

What I need is, for example, when clicking the fourth link on the page, for the values for id4 and point4 to be pulled. 我需要的是,例如,单击页面上的第四个链接时,要提取id4和point4的值。 Instead, no matter what link I click, it passes the variables for id5 and point5, which were defined last. 相反,无论我单击哪个链接,它都会传递最后定义的id5和point5变量。 I don't get it? 我不明白吗?

It is a bit nasty but you can access them like this: 有点讨厌,但是您可以像这样访问它们:

window['point' + key]

Since these are global variables, they are in fact properties of the window object, and as such they can be referred using that bracket syntax. 由于这些是全局变量,因此它们实际上是窗口对象的属性,因此可以使用该括号语法来引用它们。

Though this is equivalent to window.point5 when key == 5 , note that window.point+key would not work. 尽管这等于key == 5时的window.point5 ,但请注意window.point+key无效。

So full solution would become: 因此,完整的解决方案将变为:

var takeurl = '/api/response.json?point='+ window['point' + key] + '&id=' + window['id' + key];

Your problem is that you are creating a string point + key evaluates to the value of point plus the value of key . 您的问题是您正在创建一个字符串, point + key计算结果为point的值加上key的值。 Put what you want is the value of "point" plus key . 输入您想要的是"point" plus key的值。

so what you have to do is dynamically name the varaible that you assigned to the window object: 因此,您要做的是动态命名分配给窗口对象的变量:

window[ "point" + key ]

this will (given the key is let's say 3 ) evaluate to window.point3 which is what you need. 这将(假设关键是3 )求值到window.point3 ,这是您所需要的。

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

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