I have an Object array in my state called names
, i’m trying to make a copy of this array to another Object array in state called shuffledNames
, but when i shuffle the shuffledNames
using the code below, the original names
changes the ids to match the new shuffled ids. See images below for an example. I’ve tryed using .concat(), .slice(). I do not know what to try anymore.
handleSort = () => {
let tempName = [...this.state.names];
let shuffledNames = tempName;
for (let i = shuffledNames.length; i-- > 1; ) {
let j = Math.floor(Math.random() * i);
let temp = shuffledNames[i];
shuffledNames[i] = shuffledNames[j];
shuffledNames[j] = temp;
}
shuffledNames.forEach((shuffledName, index) => {
shuffledName.id = index;
});
this.setState({ shuffledNames: shuffledNames });
};