Invert array order

6

I have the array:

var meuArray = [1, 2, 5, 7, 9, 4];

How to invert the order so it looks like this:

[4, 9, 7, 5, 2, 1]
    
asked by anonymous 08.12.2015 / 14:10

5 answers

8

As other answers have already indicated, you can use reverse of the array. But note that this changes the original array, and there are times when you need to have a copy.

To avoid changing the original array, you can use slice before. This method is done to generate a new array that is a "slice" of the original. But if you pass 0 as the first argument, this slice will be a clone of the original whole array:

var meuArray = [1, 2, 5, 7, 9, 4];
var meuArrayInvertido = meuArray.slice(0).reverse();
    
08.12.2015 / 14:56
6

Pedro, although Array.prototype.reverse works on your example will mutate the original array, so if you make a console.log(meuArray) you will notice that it will be inverted.

var output = document.getElementById("output")
var log = function (object) {
  var container = document.createElement("div");
  container.textContent = JSON.stringify(object);
  output.appendChild(container);
}

var meuArray = [1, 2, 5, 7, 9, 4];
log(meuArray);

var meuArrayInvertido = meuArray.reverse();
log(meuArray);

log(meuArrayInvertido);
<div id="output">
</div>

In this case it does not make much sense to instantiate a meuArrayInvertido object, since the original array was also inverted. so in this case you can use Array.prototype.map to do the work.

var output = document.getElementById("output")
var log = function (object) {
  var container = document.createElement("div");
  container.textContent = JSON.stringify(object);
  output.appendChild(container);
}

var meuArray = [1, 2, 5, 7, 9, 4];
log(meuArray);

var meuArrayInvertido = meuArray.map(function (item, indice, array){
  return array[array.length - indice - 1];
});
log(meuArray);

log(meuArrayInvertido);
<div id="output">
</div>
    
08.12.2015 / 14:26
3

One way I found and using reverse

var meuArray = [1, 2, 5, 7, 9, 4];
var meuArrayInvertido = meuArray.reverse();
//meuArrayInvertido === [4, 9, 7, 5, 2, 1]
    
08.12.2015 / 14:10
3

You can use a for cycle and make element elements change:

var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
alert(array);
var length = array.length;
var left = null;
var right = null;
for (left = 0, right = length - 1; left < right; left += 1, right -= 1) {
  var temporary = array[left];
  array[left] = array[right];
  array[right] = temporary;
}
alert(array);

(See here for comparing this method to .reverse() in terms of performance).

In terms of readability and complexity, if you are dealing with arrays small and inverted a few times, .reverse() is simpler. However, if the inversion performance is critical, use the method with the cycle.

    
08.12.2015 / 14:23
3

As the TobyMosque replied, about the change in array , I developed another alternative for the occasion.

Define a function for the prototype Array , this function takes the this , which is nothing more than the variable of the type Array that calls the function, and with a for decreasing, using the function push() to creates a new array with the inverted order returning at the end of the function.

var arr = [1,2,3,4,5];

// Define uma função no prototipo do Array
// para retornar o mesmo com valores em orderm invertida.
Array.prototype.inverter = function() {
  var arr = [];
  for(i = this.length - 1; i >= 0; i--) {
     arr.push(this[i]);
  }
  return arr;
}

var nArr = arr.inverter();

// Saida de dados.
document.writeln(
  JSON.stringify(arr),
  JSON.stringify(nArr)
);
    
08.12.2015 / 14:42