循环通过一个数组,将每个值100 [英] Looping Through An Array and Dividing Each Value By 100

查看:114
本文介绍了循环通过一个数组,将每个值100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,这是我的第一个问题,我只是发现我与JS的方式。我有设置一个数组,我想通过它循环,并除以100的每个值,然后打印输出到控制台上。这里就是我到

Hey this is my first question and I'm just finding my way with JS. I have an array which is set, I want to loop through it and divide each value by 100, then print the output to the console. Here's where I'm up to

var example =[5, 10, 15, 20]

for (var i=0; i < example.length; i++) {
  (example[i/100]);
}

console.log(example);

我知道我的问题是与(例如[I / 100]); 线,但我不知道如何解决,任何帮助AP preciated : - )

I know my problem is with the (example[i/100]); line but I'm not sure how to fix, any help appreciated :-)

所以我的目标就是进入

0.5,0.1,0.15,0.2

0.5, 0.1, 0.15, 0.2

推荐答案

更新,以节省就地修改后的值

有没有太多的理由分别登录()的每个值。你可以像这样的东西精简code,以pretty ES6箭头的功能和ES5数组方式:

There's not much reason to log() each value separately. You could streamline the code with something like this, with pretty ES6 arrow functions and ES5 array methods:

var example = [5, 10, 15, 20]

console.log(example = example.map(x => x/100))

请注意:箭头的功能,如 X =&GT; X / 100 目前在Web浏览器一个非常小的一部分工作。如果你想立即发布一个Web应用程序或网页,你应该使用标准的匿名函数:函数(X){返回X / 100}

Note: arrow functions such as x => x/100 currently work in a very small subset of Web browsers. If you want to publish a web app or page right now, you should use a standard anonymous function: function (x) { return x/100 }.

这篇关于循环通过一个数组,将每个值100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆