Are there any native methods to move data around within typed arrays? For example if I want to "remove" 4 elements from somewhere within the array and then shift everything after down 4 indexes (like memmove in C). Or would this have to be written in Javascript? What do you think would be the fastest/efficient way to do this?
-
w3schools.com/jsref/jsref_obj_array.asp here you will find complete array properties and methods in javascriptAbdul Jabbar– Abdul Jabbar2014-08-29 04:17:02 +00:00Commented Aug 29, 2014 at 4:17
-
TYPED arrays, not regular arrays.pixelmike– pixelmike2014-08-29 04:22:51 +00:00Commented Aug 29, 2014 at 4:22
-
what do you mean by typed arrays? there are no typed arrays in javascript.Prabhu Murthy– Prabhu Murthy2014-08-29 04:27:43 +00:00Commented Aug 29, 2014 at 4:27
Add a comment
|
1 Answer
Well I've looked through the docs and specs that I can find, but as far as I can tell there aren't any methods to move data around within typed arrays. Here's my best guess at a "fast move" function. You'll have to ensure that the supplied ranges are valid, but it should work whether the source index is before or after the destination.
function taMove( a, iDst, iSrc, num ) {
var i, j, k;
if( iDst < iSrc ) {
// copy forward
for( i = iDst, j = iSrc, k = iDst + num; i < k; ++i, ++j ) {
a[i] = a[j];
}
}
else {
// copy backward
for( i = iDst+num-1, j = iSrc+num-1; i >= iDst; --i, --j ) {
a[i] = a[j];
}
}
}