0

I will receive the response from Axios success.

In this response, I want to save into 2 different constants with normal data and reversed data.

but while I put console both are showing the reversed ones.

how can I achieve this?

slice().reverse() also returning the same array

const [psresponse, setPSresponse] = React.useState("");
const [trackresponse, setTrackresponse] = React.useState("");

 axios.get(serverurl)
       .then(function(response) {
             var data = =response.data;
             var reverseddata = data.slice().reverse();
             setPSresponse(reverseddata);
             setTrackresponse(data);

             

             console.log(reverseddata)
             console.log(response.data)
});

I want the normal array and the reversed array.

anyone has an idea pls let me know.

4
  • "This method mutates array and is based on Array#reverse." lodash.com/docs/4.17.15#reverse Commented Feb 4, 2021 at 6:29
  • how to achieve without mutating the original array? Commented Feb 4, 2021 at 6:56
  • var reverseddata = [...data].reverse() use spread operator as both are referencing the same location Commented Feb 4, 2021 at 7:00
  • I try to spread also. but I get the same. it's not solved. Commented Feb 4, 2021 at 7:06

0