JavaScript TypedArray set() 方法用于通过从指定的源数组中读取输入值,在类型化数组中存储一个或多个值。它返回 none(undefined)。
此外,它还接受一个名为 'targetOffset' 的可选参数,该参数指定目标数组中开始写入的位置。如果未指定,它将从目标数组中的第 0 个索引开始写入。
语法
以下是 JavaScript TypedArray set() 方法的语法 -
set(typedarray, targetOffset)
参数
此方法接受两个名为 “typedarray” 和 “targetOffset” 的参数,如下所述 -
- typedarray - 要从中复制值的源数组。
- targetOffset(可选)- 目标数组中开始写入值的索引(或位置)。
返回值
此方法返回 None(undefined)。
示例 1
在下面的程序中,我们使用 JavaScript TypedArray set() 方法,通过从指定的源数组 [1, 2, 3, 4, 5, 6, 7, 8] 读取输入值,在类型化数组中存储多个值。
<html>
<head>
<title>JavaScript TypedArray set() Method</title>
</head>
<body>
<script>
const T_array = new Uint16Array([1, 2, 3, 4, 5, 6, 7, 8]);
document.write("Typed array(target array): ", T_array);
//using the set() method
document.write("<br>The set() method returns: ", T_array.set([0, 0, 0]));
</script>
</body>
</html>
输出
一旦执行了上述程序,它将返回一个 'undefined'。
The set() method returns: undefined
示例 2
如果省略 targetOffset 参数,则源数组将覆盖从索引 0 开始的目标数组中的值。
以下是 JavaScript TypedArray set() 方法的另一个示例。我们使用此方法将多个值存储在这个类型化数组 [10, 20, 30, 40, 50] 中,方法是从默认索引 0 开始从指定的源数组 [1, 2, 3] 读取输入值。
<html>
<head>
<title>JavaScript TypedArray set() Method</title>
</head>
<body>
<script>
const T_array = new Uint16Array([10, 20, 30, 40, 50]);
document.write("Typed array(target array): ", T_array);
const source_arr = new Uint16Array([1, 2, 3]);
document.write("<br>Source array: ", source_arr);
//using the set() method
T_array.set(source_arr);
document.write("<br>Typed array after set: ", T_array);
</script>
</body>
</html>
输出
上面的程序返回一个类型化数组 [1, 2, 3, 40, 50]。
Source array: 1,2,3
Typed array after set: 1,2,3,40,50
示例 3
如果我们将 targetOffset 参数值传递为 2,则它从指定的源数组开始将值存储在目标数组中,从指定的 targetOffset 值开始。
在下面给定的示例中,我们使用 JavaScript TypedArray set() 方法将多个值存储在目标类型化数组 [0,0,0,0,0,0,0,0] 中,方法是从指定的源数组 [10,20,30] 开始读取输入值从指定的索引 2 开始。
<html>
<head>
<title>JavaScript TypedArray set() Method</title>
</head>
<body>
<script>
const T_array = new Uint16Array([0, 0, 0, 0, 0, 0, 0, 0]);
document.write("Typed array(target array): ", T_array);
const source_arr = new Uint16Array([10, 20, 30]);
document.write("<br>Source array: ", source_arr);
const targetOffset = 2;
document.write("<br>The targetOffset: ", targetOffset);
//using the set() method
T_array.set(source_arr, targetOffset);
document.write("<br>Typed array after set: ", T_array);
</script>
</body>
</html>
输出
执行上述程序后,会显示如下输出 -
Source array: 10,20,30
The targetOffset: 2
Typed array after set: 0,0,10,20,30,0,0,0
示例 4
如果 targetOffset 参数值作为 −2 传递,由于它是负值,set() 方法会引发 “RangeError” 异常。
<html>
<head>
<title>JavaScript TypedArray set() Method</title>
</head>
<body>
<script>
const T_array = new Uint16Array([1, 3, 5, 7, 9]);
document.write("Typed array(target array): ", T_array);
const source_arr = new Uint16Array([10, 20, 30]);
document.write("<br>Source array: ", source_arr);
const targetOffset = -2;
document.write("<br>The targetOffset: ", targetOffset);
//using the set() method
try {
T_array.set(source_arr, targetOffset);
document.write("<br>Typed array after set: ", T_array);
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
输出
一旦执行了上述程序,它将抛出 “RangeError” 异常。
Source array: 10,20,30
The targetOffset: -2
RangeError: offset is out of bounds