stephjang
stephjang

Reputation: 959

How do I multiply each member of an array by a scalar in javascript?

For example, how do I achieve the following without iterating over the array?

var a = [1, 2, 3] * 5;  // a should equal [5, 10, 15]

Upvotes: 45

Views: 119684

Answers (10)

aakash4dev
aakash4dev

Reputation: 1176

I think it's not possible without iteration.

with iteration, here are the methods:

let a=[1,2,3,4] 

then,

method 1:

a=a.map(e=>{return e*5})

method 2:

a.forEach((e,i)=>a[i]=5*e)

Upvotes: 0

scottlittle
scottlittle

Reputation: 20822

Using Lodash's map function, this returns the original array a, multiplied by the constant 5:

_.map(a, function multiply(x) { return x * 5; });

Upvotes: 0

Leon Adler
Leon Adler

Reputation: 3331

In ECMAScript 6, you can use arrow functions:

var a = [1, 2, 3];
var b = a.map(x => x * 5); // <-------

console.log(b);   // [5, 10, 15]

Arrow functions are syntactic sugar for an inline function with lexical this binding:

// ES6
let array2 = array1.map(x => x * 5);
// ES5
var array2 = array1.map((function (x) { return x * 5; }).bind(this));

Therefore, if you need to support Internet Explorer or other old browsers (Edge understands ES6) you can use babeljs or TypeScript in your project to cross-compile your code to ES5 syntax.

Upvotes: 39

Mohammad Usman
Mohammad Usman

Reputation: 39322

As stated in Docs:

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

In my opinion, .map() is more suitable if someone wants to create a new array based on input values from the current array.

However, if someone wants to modify the array in place, .forEach() seems a better choice.

In ES6 we may use:

Following code will modify a given array arr in place (without creating a new one):

arr.forEach((value, index) => {arr[index] *= 5});

Demo:

var arr = [1, 2, 3];
var scalar = 5;

arr.forEach((value, index) => {
    arr[index] *= scalar;
});
console.log(arr);

Upvotes: 4

Ecmascript 2016 (ES7) defines SIMD mathematics which allow to do multiplications like the one you desire faster and easier. However, as of today there is very little browser support for SIMD (only Firefox nightly builds support this) [1], [2]. This is how it will look like:

var a = SIMD.Float32x4(1, 2, 3);
var b = SIMD.Float32x4(5, 5, 5);
SIMD.Float32x4.mul(a, b);  // Float32x4[5, 10, 15]

Until there will be widespread support for SIMD you'd have to resort to using map

var a = [1, 2, 3].map(function(x) { return x * 5; });

which is nicely supported by all modern browsers [3].

Upvotes: 5

Alvaro G&#243;mez
Alvaro G&#243;mez

Reputation: 39

You can use .map but you also have to create a new variable to throw the new values in:

var a = [1,2,3];

var b = a.map(function(x){
    return x * 5;
});

alert(b);

Upvotes: 2

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

for(var i=0; i<a.length; i++) {
    a[i] *= 5;
}

Upvotes: 18

BoltClock
BoltClock

Reputation: 723428

Array.map() is available to IE users as of IE9, so if you don't care about compatibility at all you can use this:

var a = [1, 2, 3].map(function(x) { return x * 5; });

For JavaScript 1.8, this is as short as you can go:

var a = [1, 2, 3].map(function(x) x * 5);

If you need maximal browser compatibility, you'll have to put up with a loop.

Either way you'll be iterating over the array; Array.map() just makes it less obvious you're doing so.

Upvotes: 47

Abdul Munim
Abdul Munim

Reputation: 19217

You can try this:

function scalarMultiply(arr, multiplier) {
   for (var i = 0; i < arr.length; i++)
   {
      arr[i] *= multiplier;
   }
   return arr;
}

USAGE

var a = scalarMultiply([1, 2, 3], 5);

Upvotes: 1

bennedich
bennedich

Reputation: 12380

var a, i, ii, term;

a = [1,2,3];
term = 5;

for (i=0, ii=a.length; i<ii; i++) {
  a[i] = a[i] * term;
}

Upvotes: 1

Related Questions