4

How do I change a JS array in place (like a Ruby "dangerous" method, e.g. with trailing !)

Example:

If I have this:

var arr = [1, 2, 3]

How can I make this:

arr === [2, 4, 6]

(assuming I have an appropriate function for doubling numbers) in one step, without making any more variables?

0

1 Answer 1

5

Use Array.prototype.forEach() , third parameter is this : input array

var arr = [1, 2, 3];
arr.forEach(function(el, index, array) {
  array[index] = el * 2
});
console.log(arr)

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.