JavaScript |类型Darray。map()和示例

这个 达雷。地图() 是JavaScript中的一个内置函数,用于创建一个新的typedArray,其结果是给定typedArray的每个元素上提供的函数。 语法:

null
typedArray.map(callback)

参数: 它接受一个参数回调函数,该函数接受下面指定的一些参数-

  • 当前值: 它是typedArray中正在处理的当前元素。
  • 索引: 它是typedArray中正在处理的当前元素的索引。
  • 数组: 这就是被称为Darray的类型。

返回值: 它返回一个新的typedArray,其中包含给定typedArray的每个元素上提供的函数的结果。

显示此函数工作的JavaScript代码:
代码#1:

<script>
// Creating a typedArray with some elements
const A = new Uint8Array([4, 9, 16, 25, 36]);
// Calling map() function with the parameter
// Math.sqrt function which find square root
// of the typedArray's elements
const B = A.map(Math.sqrt);
// Printing the result of the function
document.write(B);
</script>


输出:

2,3,4,5,6

代码#2:

<script>
// Creating a typedArray with some elements
var A = new Uint8Array([1, 2, 3, 4, 5, 6]);
// Calling map() function
var B = A.map( function (a) {
return a * 5;
});
// Returning the results
document.write(B);
</script>


输出:

5,10,15,20,25,30
© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享