Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  109] [ 2]  / answers: 1 / hits: 36627  / 8 Years ago, thu, march 3, 2016, 12:00:00

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



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?


More From » arrays

 Answers
6

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.',
});
}

[#63059] Wednesday, March 2, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parkernotnamedt

Total Points: 539
Total Questions: 90
Total Answers: 99

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
;