What I try is to check the state.newCardArray and remove that item based on index and then push it to state.arrayFoldedCards. This is a card game, so if the newCardArray is empty and there is no winner or loser, then the game stops....
const initialState = {
newCardArray: ['a', 'b', 'c']; // after removing the array looks like ['a', 'c']
arrayFoldedCards: [] // pushing to the array resulted in an updated array that looks like ['b']
}
export const game = (state = initialState, action) => {
switch (action.type) {
case types.GET_CARD:
{
const getRandomNumber = Math.floor(state.newCardArray.length * Math.random());
console.log(state.newCardArray);
console.log(state.arrayFoldedCards);
return {
...state,
randomNumber: getRandomNumber,
arrayFoldedCards: state.newCardArray[getRandomNumber].push(state.arrayFoldedCards.pop())
}
}
}
}
Looks like you want something like splice/concat
var idxToRemove = 1; //Removing 'b'
var arr0 = ['a', 'b', 'c'];
var arr1 = []; //Array to move to
return arr1.concat(arr0.splice(idxToRemove, 1));
Splice returns an array of removed elements so you can concat them together. Concat merges the two arrays without mutating either.
case types.GET_CARD: { const indexToRemove = Math.floor(state.currentCardArray.length * Math.random()); const updatedArray = state.arrayFoldedCards.concat(state.currentCardArray.splice(indexToRemove, 1)); return { ...state, arrayFoldedCards: updatedArray } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.