组成新数组的JavaScript map()方法创建一个新的类型数组,其结果是对该类型数组中的每个元素调用提供的函数。
array.map(function(value, index, arr), thisValue)
value(必需) - 当前元素的值。
index(可选) - 当前元素的数组索引。
arr(可选) - 调用了数组map()。
ThisValue(可选) - 要传递给函数的值,用作其" this"值。
一个新的数组。
Chrome | 是 |
Safari | 是 |
Firefox | 1.5 |
Opera | 是 |
JavaScript map()方法
<script type="text/javascript"> // JavaScript to illustrate map() method var input=[1,2,3]; var output=input.map(function(input) { return input*2; }); document.write("Array after using map() method the output is" ); document.write("<br>"); document.write(output); document.write("<br>"); document.write("Actual array still remain the same "); document.write("<br>"); document.write(input); // expected output: arr[Output:2,4,6] </script>
输出:
2,4,6
JavaScript map()方法
<script type="text/javascript"> // JavaScript to illustrate map() method var JavaTpoint = ['JavaTpoint','C','C++','RDBMS']; //Determine the length of each name and save it in an array var nameLengths =JavaTpoint.map(function(value, index, array) { var len =value.length; return len; }); document.write("Array using map() method the output is" ); console.log(nameLengths); document.write(nameLengths); document.write("<br>") document.write("Actual array still remain the same "); document.write(JavaTpoint); // expected output: arr[Output:10,1,3,5] </script>
输出:
10,1,3,5
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)