1

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?

3
  • w3schools.com/jsref/jsref_obj_array.asp here you will find complete array properties and methods in javascript Commented Aug 29, 2014 at 4:17
  • TYPED arrays, not regular arrays. Commented Aug 29, 2014 at 4:22
  • what do you mean by typed arrays? there are no typed arrays in javascript. Commented Aug 29, 2014 at 4:27

1 Answer 1

1

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];
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.