JavaScript TypedArray.prototype.with() Method
Last Updated :
28 May, 2024
A new method TypedArray.prototype.with(), was introduced in ECMAScript 2023 (ES2023) to provide a means of modifying an element in a particular TypedArray without altering the underlying array directly, it generates a new TypedArray with the required alteration and then returns it.
TypedArray instances with() method copies to changing a value at a particular index through bracket notation, giving back a fresh typed array where the value at the specified index is substituted with the specified one, Array.prototype.with() employs an equivalent procedure.
Syntax
TypedArray.prototype.with(index, value)
Parameter:
- index: (Required) Zero-based index at which to change the typed array, converted to an integer.
- value: (Required) Any value to be assigned to the given index.
Return Type:
A new typed array with the element index
replaced with value
.
Example 1: In this example, we are replacing an element.
JavaScript
const numbers = new Int8Array([1, 3, 5, 7, 9]);
const modifiedNumbers = numbers.with(2, 10);
console.log(numbers);
console.log(modifiedNumbers);
Output:
Int8Array(5) [1, 3, 5, 7, 9]
Int8Array(5) [1, 3, 10, 7, 9]
Example 2: In this example, we are chaining with other methods.
JavaScript
// Red, Green, Blue color values
const colors = new Uint8Array([255, 0, 0, 0, 255, 0]);
const modifiedColors = colors.with(1, 255).with(5, 255);
// Output: Uint8Array(6) [255, 0, 0, 0, 255, 0]
// (original remains unchanged)
console.log(colors);
// Output: Uint8Array(6) [255, 255, 0, 0, 255, 255]
// (both green and blue set to 255)
console.log(modifiedColors);
Output:
Uint8Array(6) [255, 0, 0, 0, 255, 0]
Uint8Array(6) [255, 255, 0, 0, 255, 255]
Supported Browsers
- Chrome
- Edge
- Firefox
- Opera
- Safari
Similar Reads
JavaScript TypedArray.prototype.values() Method TypedArray's values() method returns a new iterator object for the array, this iterator will allow you to traverse through typed arrays and generate the value of each element at every position and like values() method for normal JavaScript arrays, it serves a similar role. SyntaxtypedArray.values()P
1 min read
JavaScript TypedArray.prototype.includes() Method In JavaScript the TypedArray.prototype.includes() method is used to check that the given typed array contains a specific given element or not, if it contains the element it returns true otherwise false. SyntaxtypedArray.includes(searchElement)ORtypedArray.includes(searchElement, fromIndex)Parameters
1 min read
JavaScript TypedArray.prototype.findIndex() Method Method TypedArray.prototype.findIndex() of JavaScript returns the index of the first element in the typed array that fulfils the conditions specified by the testing function, if no elements satisfy it, -1 is returned, the findIndex() method takes a callback function as an argument and it returns an
2 min read
JavaScript TypedArray.prototype.toSorted() Method The toSorted() method, presented in ECMAScript 2023 or (ES2023) affords a secure and efficient manner of ordering elements of TypedArray in ascending order. The toSorted() method of TypedArray instances is the copying version of the sort() method, it gives back a new typed array with sorted elements
1 min read
JavaScript TypedArray.prototype.findLast() Method A new method, TypedArray.prototype.findLast(), was introduced in ECMAScript 2023 (ES2023). The method findLast() of instances TypedArray proceeds traverse over the typed array backwards and provides method returning the value of the first element that satisfies the supplied testing function. When th
2 min read