I'm trying to add a new card in the banking_cards array inside of the paymentMethods array. The banking_cards array is inside the paymentMethods array. So i wanted to insert the new card object inside the banking_cards array. My code below produces an error which says state.paymentMethods.banking_cards is not iterable.
TAKE NOTE
banking_cards array is inside the paymentMethods array
export const initialState = {
paymentMethods: [],
};
case paymentMethodConstants.ADD_CARD_SUCCESS:
return {
...state,
paymentMethods: [
...state.paymentMethods,
banking_cards: [...state.paymentMethods.banking_cards, action.payload],
],
};
JSON
paymentMethods = [
{
"id": 8,
"customer_token": "epofjoe",
"banking_cards": [
{
"id": 1,
"banking_token": "AAA",
"last_4": "0006",
"exp_year": 2021,
"exp_month": 12,
"cvc": 876
},
{
"id": 2,
"banking_token": "BBB",
"last_4": "0002",
"exp_year": 2022,
"exp_month": 12,
"cvc": 877
},
]
}
]
initialState,paymentMethodsis an array but in your reducer function, it's an object? because you havepaymentMethods: {...paymentMethodsshould be an array.paymentMethodscontains thebanking_cardswhich is also an array. I need to add the new card object inside that ofbanking_cardsbanking_cardsdoesn't mean anything. Add the structure you want to achieve.banking_cardsis an array inside ofpaymentMethodsarray. When i want to add a new card it should be inserted inside of thebanking_cardsthere.