javascript 在javascript中对数组中的每个数字进行平方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19645186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:19:36  来源:igfitidea点击:

Square each number in an array in javascript

javascript

提问by dagarre

I'm trying to square each number in an array and my original code didn't work. I looked up another way to do it, but I'd like to know WHY the original code didn't work.

我正在尝试对数组中的每个数字进行平方,但我的原始代码不起作用。我查找了另一种方法来做到这一点,但我想知道为什么原始代码不起作用。

Original code:

原始代码:

function(arr) {

    ret= [];            

    for (var i = 0, len = arr.length; i < len; i++) {
        root = Math.sqrt(arr[i]);
        ret.push(root);
    }

    return ret;
}

Working Code:

工作代码:

function(arr) {

    ret= [];

    for (var i = 0, len = arr.length; i < len; i++) {
        ret.push(arr[i] * arr[i]);
    }

    return ret;     
}

回答by Mohsen

Math.sqrtgives you square rootnot square of a number. Use Math.powwith second argument of 2.

Math.sqrt给你平方根而不是数字的平方。Math.pow与 的第二个参数一起使用2

回答by 1ace

How about that ?

那个怎么样 ?

function (arr) {
  return arr.map(function (x) {
    return Math.pow(x, 2);
  });
}

Array.map(func)applies the function to each element of the map and returns the array composed of the new values. Math.pow(base, exp)raises baseto its exppower.

Array.map(func)将该函数应用于地图的每个元素并返回由新值组成的数组。 Math.pow(base, exp)提升base到它的exp力量。

回答by John Kraft

The first sample is taking the square root, not squaring the value. To square you want to use

第一个样本是取平方根,而不是对值进行平方。要平方您要使用

Math.pow(arr[i],2);

回答by Penny Liu

Here is the function write with ES6Exponentiation(**):

这是使用ES6 取( **)编写的函数:

let arr = [1, 6, 7, 9];
let result = arr.map(x => x ** 2);
console.log(result);

回答by BASANT KUMAR

Best way to Square each number in an array in javascript

在javascript中对数组中的每个数字进行平方的最佳方法

Array.prototype.square = function () {
    var arr1 = [];
    this.map(function (obj) {
        arr1.push(obj * obj);
    });
    return arr1;
}
arr = [1, 6, 7, 9];
console.log(arr.square());
arr1 = [4, 6, 3, 2];
console.log(arr1.square())

回答by TGH

The original code is taking the square root of the value. The second version is multiplying the value with itself (squaring it). These are inverse operations

原始代码是取值的平方根。第二个版本是将值与其自身相乘(平方)。这些是逆运算

回答by Signor Siddarth

Here is how it can be done, using a simple method called .forEach

这是如何做到的,使用一个简单的方法叫做 .forEach

var numbers = [1,2,3,4,5,6,7,8];
numbers.forEach(function(element, index, array){
    array[index] = element* element;
});
console.log(numbers);

回答by Abdennour TOUMI

Use embedded for, for pretty syntax :

使用 Embedded for, 以获得漂亮的语法:

      var arr=[1,2,3,4] ;
      [for (i of arr) i*i ]; 

      //OUT : > [1,4,9,16]

回答by Ad_

let arr = [1, 2, 3];
let mapped = arr.map(x => Math.pow(x, 2));
console.log(mapped);

回答by Mohan Ram

Declarative Programming :)

声明式编程 :)

let list = [1,2,3,4,5,6,7,8,9,10];
let result = list.map(x => x*x);
console.log(result);