@stdlib/array
Version:
Arrays.
90 lines (66 loc) • 2.02 kB
Plain Text
{{alias}}( x, index, value )
Returns a new array with the element at the specified index replaced with a
provided value.
Negative indices are resolved relative to the last array element, with the
last element corresponding to `-1`.
If provided an array-like object having a `with` method , the function
defers execution to that method and assumes that the method has the
following signature:
x.with( index, value )
If provided an array-like object without a `with` method, the function
shallow copies input array data to a new generic array, normalizes a
provided index, and sets a specified element.
Parameters
----------
x: ArrayLikeObject
Input array.
index: integer
Index of the element to be replaced.
value: any
Replacement value.
Returns
-------
out: ArrayLikeObject
Output array.
Examples
--------
> var x = [ 1, 2, 3, 4 ];
> {{alias}}( x, 0, 5 )
[ 5, 2, 3, 4 ]
> {{alias}}( x, -1, 6 )
[ 1, 2, 3, 6 ]
> x
[ 1, 2, 3, 4 ]
{{alias}}.assign( x, index, value, out, stride, offset )
Copies elements from one array to another array and sets the element at the
specified index to a provided value.
Negative indices are resolved relative to the last array element, with the
last element corresponding to `-1`.
Parameters
----------
x: ArrayLikeObject
Input array.
index: integer
Index of the element to be replaced.
value: any
Replacement value.
out: ArrayLikeObject
Output array.
stride: integer
Output array stride.
offset: integer
Output array offset.
Returns
-------
out: ArrayLikeObject
Output array.
Examples
--------
> var x = [ 1, 2, 3, 4 ];
> var out = [ 0, 0, 0, 0 ];
> var arr = {{alias}}.assign( x, 0, 5, out, 1, 0 )
[ 5, 2, 3, 4 ]
> var bool = ( arr === out )
true
See Also
--------