Looping Through An Array and Dividing Each Value By 100

后端 未结 5 1278
Happy的楠姐
Happy的楠姐 2021-01-27 03:44

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 t

5条回答
  •  青春惊慌失措
    2021-01-27 04:40

    The non-ES6 code might read like this. map returns a new array where each element has been changed by the function. It's up to you whether you want a new array, or whether you use a for loop to change the elements of the existing array.

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

    Example 1

    var out = example.map(function (el) {
        return el / 100;
    });
    
    console.log(out); // [ 0.05, 0.1, 0.15, 0.2 ]
    

    Example 2

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

提交回复
热议问题