EN
JavaScript - multiply each item of array by scalar?
1
answers
0
points
How can I multiply each item of an array by a scalar in JavaScript?
I need something like:
var array = [1, 2, 3] * 10;
To get array consisting of:
[10, 20, 30]
1 answer
0
points
You can use map()
method to do this.
Practical examples
ES6+ solution:
// ONLINE-RUNNER:browser;
const array = [1, 2, 3];
const result = array.map((x) => x * 10);
console.log(result); // [ 10, 20, 30 ]
For older versions of JavaScript:
// ONLINE-RUNNER:browser;
var array = [1, 2, 3];
var result = array.map(function (x) {
return x * 10;
});
console.log(result); // [ 10, 20, 30 ]
References
0 comments
Add comment