JavaScript - TypedArray with() Method
The JavaScript TypedArray with() method is used change the value of a given index. It returns a new typed array with the element at index replaced with the specified value.
This method throws a 'RangeError' exception, if the index parameter value is either less than or greater than the typed array length.
Syntax
Following is the syntax of JavaScript TypedArray with() method −
arrayInstance.with(index, value)
Parameters
index − The zero-based index at which the value will be will replaced or changed.
value − A value to be insert or assigned at the specified index.
Return value
This method returns a new typed array with the element at specified index replaced with value.
Examples
Example 1
Replace the value of first element (index = 0), with the specified value.
In the following example we are using the JavaScript TypedArray with() method to change (or replace) this typed array [10, 20, 30, 40, 50] element at specified index 0 with the value 10.
<html>
<head>
<title>JavaScript TypedArray with() Method</title>
</head>
<body>
<script>
const T_array = new Uint8Array([10, 20, 30, 40, 50]);
document.write("Typed array: ", T_array);
const index = 0;
const value = 0;
document.write("<br>Index: ", index);
document.write("<br>Value: ", value);
var new_arr = ([]);
//using with() method
new_arr = T_array.with(index, value);
document.write("<br>New typed array after replacing value at specified index: ", new_arr);
</script>
</body>
</html>
Output
Once the above program is executed, it will returns a new typed array after replacing [0, 20, 30, 40, 50].
Typed array: 10,20,30,40,50 Index: 0 Value: 0 New typed array after replacing value at specified index: 0,20,30,40,50
Example 2
The following is another example of the JavaScript TypedArray with() method. We use this method to change (or replace) the value of this typed array [1, 2, 3, 4, 5, 6, 7] at specified index 3 with the given value 10.
<html>
<head>
<title>JavaScript TypedArray with() Method</title>
</head>
<body>
<script>
const T_array = new Uint8Array([1, 2, 3, 4, 5, 6, 7] );
document.write("Typed array: ", T_array);
const index = 3;
const value = 10;
document.write("<br>Index: ", index);
document.write("<br>Value: ", value);
var new_arr = ([]);
//using with() method
new_arr = T_array.with(index, value);
document.write("<br>New typed array after replacing value at specified index: ", new_arr);
</script>
</body>
</html>
Output
The above program returns a new typed array as [1,2,3,10,5,6,7].
Typed array: 1,2,3,4,5,6,7 Index: 3 Value: 10 New typed array after replacing value at specified index: 1,2,3,10,5,6,7
Example 3
If the index parameter value is greater than the typed array length, it will throw a 'RangeError' exception.
<html>
<head>
<title>JavaScript TypedArray with() Method</title>
</head>
<body>
<script>
const T_array = new Uint8Array([10, 20, 30, 40, 50, 60, 70, 80]);
document.write("Typed array: ", T_array);
const index = 10;
const value = 1;
document.write("<br>Index: ", index);
document.write("<br>Value: ", value);
document.write("<br>Typed array length: ", T_array.length);
const new_arr = ([]);
try {
new_arr = T_array.with(index, value);
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
Output
After executing the above program, it will throw a 'RangeError' exception.
Typed array: 10,20,30,40,50,60,70,80 Index: 10 Value: 1 Typed array length: 8 RangeError: Invalid typed array index