如何在javascript中将数组的每个成员乘以标量?

时间:2021-05-10 13:43:29

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]

9 个解决方案

#1


29  

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

从IE9开始,IE用户可以使用Array.map(),所以如果您根本不关心兼容性,可以使用:

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

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

对于JavaScript 1.8,这是最短的:

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.

无论哪种方式,你都会迭代数组; Array.map()只是让你不那么明显。

#2


13  

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

#3


9  

Fast-forward to 2016, when support for ECMAScript 6 is getting better:

快进到2016年,当ECMAScript 6的支持越来越好时:

If you use babeljs or TypeScript in your project to cross-compile your code to ES5 or you don't need to support old browsers and Internet Explorer (Edge has support), you can use ES6 arrow functions:

如果在项目中使用babeljs或TypeScript将代码交叉编译为ES5,或者您不需要支持旧浏览器和Internet Explorer(Edge支持),则可以使用ES6箭头函数:

var a = [1, 2, 3];

var b = a.map(x => x * 5);
//            ^^^^^^^^^^

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

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

箭头函数是内联函数的语法糖,具有词法结合:

// ES6
array2 = array.map(x => x * 5);

// ES5
array2 = array.map((function (x) { return x * 5; }).bind(this));

#4


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:

Ecmascript 2016(ES7)定义了SIMD数学,它允许像你想要的那样快速和简单地进行乘法运算。但是,到目前为止,浏览器对SIMD的支持很少(只有Firefox夜间构建支持这一点)[1],[2]。这是它的样子:

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

在对SIMD提供广泛支持之前,您不得不求助于使用地图

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

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

所有现代浏览器都很好地支持[3]。

#5


2  

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

您可以使用.map,但您还必须创建一个新变量以将新值抛出:

var a = [1,2,3];

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

alert(b);

#6


1  

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;
}

#7


1  

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);

#8


0  

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.

map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

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

在我看来,如果有人想根据当前数组的输入值创建一个新数组,.map()更合适。

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

但是,如果有人想要修改数组,.forEach()似乎是一个更好的选择。

In ES6 we may use:

在ES6中,我们可以使用:

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

以下代码将修改给定的数组arr(不创建新数组):

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);

#9


0  

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

使用Lodash的map函数,返回原始数组a,乘以常量5:

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

#1


29  

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

从IE9开始,IE用户可以使用Array.map(),所以如果您根本不关心兼容性,可以使用:

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

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

对于JavaScript 1.8,这是最短的:

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.

无论哪种方式,你都会迭代数组; Array.map()只是让你不那么明显。

#2


13  

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

#3


9  

Fast-forward to 2016, when support for ECMAScript 6 is getting better:

快进到2016年,当ECMAScript 6的支持越来越好时:

If you use babeljs or TypeScript in your project to cross-compile your code to ES5 or you don't need to support old browsers and Internet Explorer (Edge has support), you can use ES6 arrow functions:

如果在项目中使用babeljs或TypeScript将代码交叉编译为ES5,或者您不需要支持旧浏览器和Internet Explorer(Edge支持),则可以使用ES6箭头函数:

var a = [1, 2, 3];

var b = a.map(x => x * 5);
//            ^^^^^^^^^^

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

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

箭头函数是内联函数的语法糖,具有词法结合:

// ES6
array2 = array.map(x => x * 5);

// ES5
array2 = array.map((function (x) { return x * 5; }).bind(this));

#4


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:

Ecmascript 2016(ES7)定义了SIMD数学,它允许像你想要的那样快速和简单地进行乘法运算。但是,到目前为止,浏览器对SIMD的支持很少(只有Firefox夜间构建支持这一点)[1],[2]。这是它的样子:

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

在对SIMD提供广泛支持之前,您不得不求助于使用地图

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

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

所有现代浏览器都很好地支持[3]。

#5


2  

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

您可以使用.map,但您还必须创建一个新变量以将新值抛出:

var a = [1,2,3];

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

alert(b);

#6


1  

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;
}

#7


1  

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);

#8


0  

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.

map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

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

在我看来,如果有人想根据当前数组的输入值创建一个新数组,.map()更合适。

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

但是,如果有人想要修改数组,.forEach()似乎是一个更好的选择。

In ES6 we may use:

在ES6中,我们可以使用:

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

以下代码将修改给定的数组arr(不创建新数组):

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);

#9


0  

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

使用Lodash的map函数,返回原始数组a,乘以常量5:

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