3

Is there a function like map() in JS that stores the returns in the original array, instead of making a new array? If not, what would be the most efficient way to do this?

14
  • You can use forEach. Commented Aug 4, 2016 at 20:08
  • 2
    The idea behind map is that you can modify the data contained within an array without modifying the original data. Commented Aug 4, 2016 at 20:09
  • 1
    @RichardChristensen the OP appears to know this, and why bother creating a new array if you don't need one ? Commented Aug 4, 2016 at 20:10
  • 2
    @Redu no, it isn't - that has created a new array, which leaves any other element holding a reference to that array now holding a reference to a different array. Commented Aug 4, 2016 at 20:15
  • 1
    @Redu that fixes the side effect problem, but still creates (and then throws away) a new array, unnecessarily. Please read my answer. Commented Aug 4, 2016 at 20:22

1 Answer 1

6

Just use .forEach() instead:

myArray.forEach(function(value, index, array) {
    array[index] = ...   // some mutation of value
});

The exact same code would still work with .map (which invokes the callback with the same three parameters and therefore allows in-place mutation) but .forEach avoids the overhead of creating a new array, only to have it thrown away again immediately.

Note also that whilst one could just refer to myArray inside the callback, it's much more efficient not to. Having the array parameter passed to the callback allows the callback to manipulate or access the original array without requiring it to be in the lexical scope (although using this to insert or delete elements would be ill advised)

Sign up to request clarification or add additional context in comments.

4 Comments

Great answer! I just want to add that the reason it's more efficient to use the array variable from the arguments is because many JavaScript engines optimize functions that don't reference lexical scopes by omitting variable lookups in the function definition's lexical scopes. You can even confirm this in a browser debugger by setting a breakpoint inside this function and trying to access a lexical variable. It'll error.
Good answer. The only addendum I would make is that this approach allows one to not only alter the value on individual elements of the array, but also to add or remove elements from the array while iterating over it. One should do this with caution, because the behavior may or may not be what you expect (and may vary between implementations).
@SMcCrohan I think the spec guarantees that the array indices are determined just once before iteration starts, but I would never recommend actually changing the array size during such a loop.
Right. I definitely wasn't intending to ENDORSE the practice...more like 'call out the possibility so as to have an opportunity to warn against it' :)

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.