React - Redux, how can I modify a complex object?

Asked

Viewed 172 times

1

I am trying to modify some properties of an array through an index coming from mine payload.

The code below goes through the array looking for the index and tries to modify its properties. This even works for less complex objects. I made it based on those answers.

This is the structure of my object.

    {
      "items": [
        {
          "sellers": [
            {
              "commertialOffer": {
                 Price: 0,
                 ListPrice: 0,
                 AvailableQuantity: 0
               }
            }]
        }]
    }

What should I do to modify the properties of commertialOffer?

I’m coming from the Vue and I’ve suffered a little with React.

I have received some errors with Unexpected token, expected ","

case 'setTotalEstoque':
      return {
        ...state, 
        arrayFavorito: state.contents.map((content, i) => 
          i === action.payload.indice ? {
            ...content,
              items[0].sellers[0].commertialOffer.Price: action.payload.price,
              items[0].sellers[0].commertialOffer.ListPrice: action.payload.listPrice,
              items[0].sellers[0].commertialOffer.AvailableQuantity: item.AvailableQuantity,
          }: content
        )

      }

1 answer

2


Do as follows, opening key inside the function and doing the assignment manually.

case 'setTotalEstoque':
  const arrayFavorito = state.contents.map((content, i) => {
    if(i === action.payload.indice) {
      content.items[0].sellers[0].commertialOffer.Price= action.payload.price,
      content.items[0].sellers[0].commertialOffer.ListPrice= action.payload.listPrice,
      content.items[0].sellers[0].commertialOffer.AvailableQuantity= action.payload.AvailableQuantity,
    }

    return content;
  });

  return {
    ...state, 
    arrayFavorito
  }
  • I put a second way, improving the organization.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.