为什么我不能在foreach中使用这样的添加?

Str*_*rry 1 javascript

Javascript的foreach等价物:

for ( var i in products )
{
    document.write("Write" + i + 1 );
}
Run Code Online (Sandbox Code Playgroud)

输出:

Write01
Run Code Online (Sandbox Code Playgroud)

编辑: 我尝试解析i为整数.

for ( var i in products )
{
    document.write("Write" + parseInt(i) + 1 );
}
Run Code Online (Sandbox Code Playgroud)

Bol*_*wyn 10

因为i是一个数字,但是第一个被转换为字符串+.用这个:

for ( var i in products ) {
  document.write(""); document.write("Write" + (i + 1) );
  // ------------------------------------------^
}
Run Code Online (Sandbox Code Playgroud)