我试图在这样的对象中操纵属性:
for (property in posts.data) {
property = property+1+",";
output += property;
}
document.write(output);
Run Code Online (Sandbox Code Playgroud)
属性是数字:0,1,2等.
在这种情况下,我想得到结果1,2,3.但相反,我得到01,11,21等...它似乎将属性视为文本字符串而不是数字.为什么?我能做些什么呢?
1.一元'+'运算符将字符串转换为整数
Run Code Online (Sandbox Code Playgroud)for (property in posts.data) { var t = +property + 1; output += t+","; }
2.Javascript的parseInt方法
Run Code Online (Sandbox Code Playgroud)for (property in posts.data) { var t = parseInt(property, 10)+ 1; output += t+","; }
parseInt函数调用(radix)中的第二个参数将告诉函数要遵循的数字系统.
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |