Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
Filter an array of objects and add a new property to each object with Vue 3 and Vuex 4

To retrieve a list of objects in a Vue 3 component I use a getters in Vuex 4 like this:

    getAccountingEntries: (state) => {
    let balance = 0;
    return state.entries.filter((entry) => {
      balance = balance + (entry.revenue - entry.expenditure);
      entry.balance = balance;
      return (
        entry.type === 'gj' &&
        entry.year === state.yearDisplayed &&
        entry.userID === state.currentUserID
      );
    });
  }

To retrieve a list of objects in a Vue 3 component I use a getters in vuex 4 like this:

You will notice that I perform a calculation and add the result in a new property named balance.

It works perfectly.

But I noticed that adding the balance property on the object also reflected in the objects in state.entries.

I don't like my getAccountingEntries function to change state without going through a mutation although it seems to work fine.

Am I doing it wrong? Is there a way to avoid this side effect?

Thank you for your help.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda