简体   繁体   English

串联似乎在 JavaScript 中不起作用

[英]Concatenation doesn't seem to work in JavaScript

I am doing something like this in my application.我在我的应用程序中做这样的事情。 I have sequence of Id values like ID1, ID2 etc. I am trying to fetch the ID value using for loop.我有一系列 ID 值,如 ID1、ID2 等。我正在尝试使用 for 循环获取 ID 值。

Concatenation doesn't seem to work.串联似乎不起作用。

 function Save(count,Id1,Id2,Id3){ var response = []; for(var i=1;i <= count; i++) { value = `${'Id' + i}`; alert(value); } }
 <input type="button" value="Submit" onclick="Save(3,1,2,3)" />

You're creating a String "Id1" , you cannot interpolate a variable name.您正在创建一个 String "Id1" ,您不能插入一个变量名。 But with ES6, you can use the rest parameters ( ... ) to convert parts of your arguments to an Array:但是使用 ES6,您可以使用rest 参数( ... ) 将 arguments 的部分转换为数组:

 function Save(count, ...ids) { var response = []; for (var i = 0; i < count; i++) { value = ids[i]; alert(value); } }
 <input type="button" value="Submit" onclick="Save(3,1,2,3)" />

Before ES6, you would have to use arguments (that would convert all arguments):在 ES6 之前,您必须使用arguments (这将转换所有参数):

 function Save() { var response = []; for (var i = 1; i <= arguments[0]; i++) { value = arguments[i]; alert(value); } }
 <input type="button" value="Submit" onclick="Save(3,1,2,3)" />

Or use eval , but don't.或使用eval ,但不要。 This is just for the example:这仅用于示例:

 function Save(count, Id1, Id2, Id3, Id4) { var response = []; for (var i = 1; i <= count; i++) { value = eval(`Id${i}`); alert(value); } }
 <input type="button" value="Submit" onclick="Save(3,1,2,3)" />

That doens't works because will throw a syntax exception.这不起作用,因为会引发语法异常。 Its not the way you use interpolation.它不是您使用插值的方式。 There are lots of ways to do what you need in a simple way:有很多方法可以以简单的方式完成您需要的工作:

  • Using an array:使用数组:

 function Save(ids) { var response = []; for(var i=0;i < ids.length; i++) { value = ids[i]; console.log(value); } } Save([1,2,3,4]);

  • Using arguments constant:使用arguments常量:

 function Save(){ var response = []; for(var i=0;i < arguments.length; i++) { value = arguments[i]; console.log(value); } } Save(1,2,3,4);

That's not how interpolation works.这不是插值的工作方式。 It just inserts the value of the expression into a string.它只是将表达式的值插入到字符串中。 For example:例如:

const age = 30
console.log(`I am ${age} years old!`) // Prints "I am 30 years old"

It doesn't interpret a string as an expression, which is what you apparently want to do.它不会将字符串解释为表达式,这显然是您想要做的。 You could do that using eval , but this is most likely a step into the wrong direction.可以使用eval来做到这一点,但这很可能是朝着错误方向迈出的一步。

If you want to iterate over the given values, just use an array instead:如果要遍历给定值,只需使用数组即可:

 function save (values) { for (const value of values) { alert(value) } }
 <input type="button" value="Submit" onclick="save([9, 8, 7])" />

You can also iterate manually over the indexes, that can be useful if you need the index too, for example:您还可以手动迭代索引,如果您也需要索引,这可能很有用,例如:

 function save (values) { for (let i = 0; i < values.length; i++) { alert(`Index ${i} = ${values[i]}`) } }
 <input type="button" value="Submit" onclick="save([9, 8, 7])" />

Or, if you want to keep your method of calling the function with multiple arguments (although the count argument is completely unnecessary), you can destructure the arguments into an array, like this:或者,如果您想保留使用多个 arguments 调用 function 的方法(尽管count参数完全没有必要),您可以将 arguments 解构为一个数组,如下所示:

 function save (...values) { for (let i = 0; i < values.length; i++) { alert(`Index ${i} = ${values[i]}`) } }
 <input type="button" value="Submit" onclick="save(9, 8, 7)" />

If you really wanted that count argument there, you could use function save (count, ...values) .如果你真的想要那个count参数,你可以使用function save (count, ...values) As a side note, there is also arguments , which is an array-like object that holds all the arguments passed to your current function, so you could iterate over that as well. As a side note, there is also arguments , which is an array-like object that holds all the arguments passed to your current function, so you could iterate over that as well.

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

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