2017-09-05 32 views
0

我有一个结尾不同,如连接字符串,使可变

fibrecementrustedorpoppedfixings 

fibrecementrustedorpoppedfixingsprice 

fibrecementrustedorpoppedfixingsunits 

fibrecementrustedorpoppedfixingsrange 

我正在寻找一种方式变多变量能够只使用主变量,然后添加在哪里所需的最后一部分在功能上类似

fibrecementrustedorpoppedfixings +价格将打印fibrecementrustedorpoppedfixingsprice的价值

+0

您可能想要使用数组 - https://www.w3schools.com/js/js_arrays.asp –

+5

可能重复[转换字符串到JavaScript中的变量名称](https://stackoverflow.com/questions/5613834/convert-string-to-variable-name-in-javascript) – Stijn

+0

在字符串上使用'eval' – Jaya

回答

-1

什么你要找的是eval()

var fibrecementrustedorpoppedfixingsprice = 23; 
 
console.log(eval("fibrecementrustedorpoppedfixings" + "price"));

然而,注意eval()can be evil,因为它:

  • 需要编译(因此慢)
  • 可以执行恶意参数
  • 继承执行范围

因此,只能谨慎使用。

希望这会有所帮助! :)

+0

这看起来工作完美谢谢!尽管如此,我会更加注意eval() – NZLRhyz