Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Reverse is not working for me ...

Consider the following code (React JS code):

  poll() {
    var self   = this;
    var url    = "//" + location.hostname + "/api/v1/eve/history/historical-data/" + this.state.itemId + '/' + this.state.regionId + '/40';

    $.get(url, function(result) {
      console.log(result.data, result.data.reverse());
      self.setState({
        error:          null,
        historicalData: result.data.reverse(),
        isLoading: false
      });
    }).fail(function(response) {
      self.setState({
        error: 'Could not fetch average price data. Looks like something went wrong.',
      });
    });
  }

Notice the console.log. Lets see an image:

enter image description here

Last I checked, reverse should have reversed the order of the array. Yet it doesn't.

Am I Using this wrong (official MDN Docs)? Why isn't reverse working?

like image 980
TheWebs Avatar asked Mar 03 '16 18:03

TheWebs


People also ask

How do you reverse data in an array?

The reverse() method reverses the order of the elements in an array. The reverse() method overwrites the original array.

Can we reverse arrays?

Using SwappingInstead, we reverse the original array itself. In this method, we swap the elements of the array. The first element is swapped with the last element. The second element is swapped with the last but one element and so on.

How do you reverse an array without any existing function?

In this method, the idea is to use a negative sign but by storing it into a variable. By using this statement x = (INT_MIN/INT_MAX), we get -1 in a variable x. As INT_MIN and INT_MAX have same values just of opposite signs, so on dividing them it will give -1. Then 'x' can be used in decrementing the index from last.


2 Answers

It has reversed it, the reverse() is executed before the console.log(). It mutates the actual array first in place returning a reference so then when it is logged, a is also reversed.

var a = [1,2,3,4];
console.log(a, a.reverse());
// [4, 3, 2, 1] [4, 3, 2, 1]

Everything inside the console.log parens is evaluated first. Try 2 reverses, can you guess what happens, it goes back to original order, like in your example.

var a = [1,2,3,4]
console.log(a, a.reverse());
// [4, 3, 2, 1] 
like image 188
Matt Harrison Avatar answered Oct 28 '22 18:10

Matt Harrison


As described at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse, reverse() reverses the order of an array in place, so the array is reversed after it's been called. You're calling it twice, resulting in the array being restored to its original order. Try this:

poll() {
    var self   = this;
    var url    = "//" + location.hostname + "/api/v1/eve/history/historical-data/" + this.state.itemId + '/' + this.state.regionId + '/40';

    $.get(url, function(result) {
        result.data.reverse();
        console.log(result.data, result);
        self.setState({
            error:          null,
            historicalData: result,
            isLoading: false
        });
    }).fail(function(response) {
        self.setState({
            error: 'Could not fetch average price data. Looks like something went wrong.',
    });
}
like image 23
AmericanUmlaut Avatar answered Oct 28 '22 17:10

AmericanUmlaut