JavaScript Array 对象 实例 返回一个数组,数组中元素为原始数组的平方根: varnumbers = [4,9,16,25]; functionmyFunction() { x = document.getElementById("demo") x.innerHTML = numbers.map(Math.sqrt); } 输出结果为: 2,3,4,5 尝试一下 » ...
array.map(function() {},this) 的作用实际上和 array.map(function() {}.bind(this)) 是一样的。map的第二个参数就是给第一个参数bind一个对象,这样在第一个参数里面就可以用this代替第二个参数。 回到你的题目中,前面第一个this其实就是指向了window,而function里面的this指向的是map的第二个参数,所以...
// The obj argument specifies the this value in the callback function. var result = numbers.map(obj.remainder, obj); document.write(result); // Output: // 6,2,5,0示例 在下面的示例中,内置 JavaScript 方法用作回调函数。 // Apply Math.sqrt(value) to each element in an array. var ...
一种方法是遍历这个数组,做立方计算后赋给新的数组,像下面这样: 1'use strict';2functionmySquare(arg){3//return arg * arg * arg;4returnMath.pow(arg,3);5}6varmyArr = [1,2,3,4,5,6,7];7varmyNewArr =[];8for(variinmyArr){9myNewArr[i] =mySquare(myArr[i]);10}11console.log(m...
语法:array.map(function(value,index,array){return...})value:必须。当前元素的值index:可选。当前元素的索引值array:可选。当前元素属于的数组对象 实例: vararr=[1,2,3,4,5];varsds=arr.map(function(value,index,array){returnvalue*2})console.log(sds)//2, 4, 6, 8, 10 ...
map()does not change the original array. map()executescallbackonce for each array element in order. map()does not executecallbackfor array elements without values. Example 1: Mapping array elements using custom function constprices = [1800,2000,3000,5000,500,8000]; ...
Multiply all the values in an array with 10: constnumbers = [65,44,12,4]; constnewArr = numbers.map(myFunction) functionmyFunction(num) { returnnum *10; } Try it Yourself » More examples below. Description map()creates a new array from calling a function for every array element. ...
Return Value: An Array containing the results of calling the provided function for each element in the original array. JavaScript Version: 1.6More ExamplesExample Multiply all the values in array with a specific number: var numbers = [65, 44, 12, 4];function multiplyArrayElement(num) { ...
array:可选参数,表示正在处理的当前数组。 thisArg:可选参数,表示执行 callback 函数时的 this 值。 map()的基本使用 ⭐使用map()方法将数组中的数字乘以 2 并返回新的数组: let numbers = [1, 2, 3, 4];let doubled = numbers.map(function(num) {return num * 2;});console.log(doubled); /...
thisArgument — 这是在执行 callBackFunction 时用作 this 的值。 1、将数组元素加倍 您可以使用 map() 方法从另一个数组创建一个新数组。例如,您可以将整数数组的元素加倍并从初始数组构造一个新数组。 letinitialArray = [1,2,3,4,5] letdoubles = initialArr...