Changing an array in javascript

I have a Javascript function called reverseArray that takes an array as an argument and returns a new array that has the same values ​​as the input array in reverse order. I want to create a function reverseArryInPlace that will change the value of the input array in reverse order.

function reverseArray(inputArray) { var outputArray = []; for (var i = inputArray.length - 1; i >= 0; i--) outputArray.push(inputArray[i]); return outputArray; } function reverseArrayInPlace(inPlaceInputArray) { inPlaceInputArray = reverseArray(inPlaceInputArray); console.log('Inside reverseArrayInPlace: ' + inPlaceInputArray); return inPlaceInputArray; } var arrayValue = [1, 2, 3, 4, 5]; reverseArrayInPlace(arrayValue); console.log('Outside reverseArrayInPlace: ' + arrayValue); // Expected Value: [5, 4, 3, 2, 1] 

Here is the result that I get when I execute this piece of code:

 Inside reverseArrayInPlace: 5,4,3,2,1 Outside reverseArrayInPlace: 1,2,3,4,5 

Inside the reverseArrayInPlace function, the arrayValue variable was canceled as expected. Why, when I refer to the same variable outside the reverseArrayInPlace function, does it return to the original order?

+5
source share
4 answers

If you want to flip it into place, you need to cancel it.

 function reverseArrayInPlace(array) { for (let i = 0, j = array.length - 1; i < j; i++, j--) [array[i], array[j]] = [array[j], array[i]]; } const a = [1,2,3,4,5]; reverseArrayInPlace(a); console.log(a); 
+3
source

The main problem is that primitives are passed by value in JavaScript. See the following question for more details:

Javascript by reference or by value

As a simple example, this is a function that tries to mutate a string that was passed to it:

 var outsideValue = 'foo' function mutate(value) { value = 'fish' } mutate(outsideValue); console.log(outsideValue); 

However, console output is foo .

This is because the value variable in the mutate function is a variable that has a reference to outsideValue when the function is initially called. When it is assigned a new value inside the function body, it simply changes the value variable to refer to a new line. As a result, outsideValue is untouched.

Check this answer for an example that changes location:

fooobar.com/questions/1266890 / ...

Note that it does not reassign the function argument.

+2
source

As others have said, arrays are objects, so they are passed by reference. You should change the original array, not create a new one.

So here is another version of reverseInPlace, it uses shift to remove the last element from the array and splicing to insert it in a new location:

 function reverseInPlace(arr) { var i = arr.length; while (i--) { arr.splice(i, 0, arr.shift()); } } var arr = [1,2,3,4,5]; console.log('Before: ' + arr.join()); reverseInPlace(arr); console.log('After: ' + arr.join()); 

For some fun, you can also use sorting:

NB : this only works in some browsers, it depends on the built-in sorting algorithm, depending on the implementation.

 function reverseInPlace(arr) { arr.sort(() => 1); } var arr = [47, 95, 80, 62, 8, 34, 31, 17, 62, 17, 85, 72, 51, 20, 68, 60, 30, 84, 7, 34]; console.log('Before: ' + arr.join()); reverseInPlace(arr); console.log('After : ' + arr.join()); 
0
source
 function reverseArray(inputArray) { var outputArray = []; for (var i = inputArray.length - 1; i >= 0; i--) outputArray.push(inputArray[i]); return outputArray; } function reverseArrayInPlace(inPlaceInputArray) { inPlaceInputArray = reverseArray(inPlaceInputArray); console.log('Inside reverseArrayInPlace: ' + inPlaceInputArray); return inPlaceInputArray; } var arrayValue = [1, 2, 3, 4, 5]; **arrayValue = reverseArrayInPlace(arrayValue);** alert('Outside reverseArrayInPlace: ' + arrayValue); // Expected Value: [5, 4, 3, 2, 1] 

//

your code is correct, you just need to replace one line and write arrayValue = reverseArrayInPlace (arrayValue); instead of reverseArrayInPlace (arrayValue); Then arratValue will print the expected values

-1
source

Source: https://habr.com/ru/post/1266890/


All Articles