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:
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?
Advertisement
Answer
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.', }); }