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

126
Visualizações
Filter an array of products by months

I am doing a project in vue and I have a json with array inside array. I want to make a filter of months that in function to the months that I select shows the list of products with that filter

And I have a selector with several months (6, 12, 18) How can I show only the products that I have selected with the selector?

 [
      {
        "active": true,
        "installment": [
          [
            {
              "product": "Ipad",
              "months": 12,
              "installment": 63.94
            }
          ],
          [
            {
              "product": "Ipad",
              "months": 6,
              "installment": 63.94
            }
          ]
        ]
      },
      {
        "active": true,
        "installment": [
          [
            {
              "product": "Iphone",
              "months": 12,
              "installment": 63.94
            }
          ],
          [
            {
              "product": "Iphone",
              "months": 6,
              "installment": 63.94
            }
          ]
        ]
      },
      {
        "active": true,
        "installment": [
          [
            {
              "product": "Mac",
              "months": 18,
              "installment": 63.94
            }
          ],
          [
            {
              "product": "Mac",
              "months": 24,
              "installment": 63.94
            }
          ]
        ]
      }
    ]

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can iterate through your array, and then push the elements which passes the criteria to a new array, it would look something like this

const array = [
    {
        "active": true,
        "installment": [
            [
                {
                    "product": "Ipad",
                    "months": 12,
                    "installment": 63.94
                }
            ],
            [
                {
                    "product": "Ipad",
                    "months": 6,
                    "installment": 63.94
                }
            ]
        ]
    },
    {
        "active": true,
        "installment": [
            [
                {
                    "product": "Iphone",
                    "months": 12,
                    "installment": 63.94
                }
            ],
            [
                {
                    "product": "Iphone",
                    "months": 6,
                    "installment": 63.94
                }
            ]
        ]
    },
    {
        "active": true,
        "installment": [
            [
                {
                    "product": "Mac",
                    "months": 18,
                    "installment": 63.94
                }
            ],
            [
                {
                    "product": "Mac",
                    "months": 24,
                    "installment": 63.94
                }
            ]
        ]
    }
]

let res = [];
// const filter = (y) => y.months == 6;
// if you have multiple filters
const filter = (y) => [6, 12, 18].includes(y.months);
array.forEach(x => {
    x.installment.forEach(y => {
        y[0].active = x.active;
        if (filter(y[0])) res = [...res, ...y];
    })
})

console.log(res);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can just filter them

const data = [
  {
    "active": true,
    "installment": [
      [
        {
          "product": "Ipad",
          "months": 12,
          "installment": 63.94
        }
      ],
      [
        {
          "product": "Ipad",
          "months": 6,
          "installment": 63.94
        }
      ]
    ]
  },
  {
    "active": true,
    "installment": [
      [
        {
          "product": "Iphone",
          "months": 12,
          "installment": 63.94
        }
      ],
      [
        {
          "product": "Iphone",
          "months": 6,
          "installment": 63.94
        }
      ]
    ]
  },
  {
    "active": true,
    "installment": [
      [
        {
          "product": "Mac",
          "months": 18,
          "installment": 63.94
        }
      ],
      [
        {
          "product": "Mac",
          "months": 24,
          "installment": 63.94
        }
      ]
    ]
  }
]

const filtered = data.map((item) => {
  const { installment } = item;
  const filteredInstallment = installment.map((item) => {
    const filteredProducts = item.filter((product) => product.months === 6)
    return filteredProducts;
  });

  return filteredInstallment;
});

console.log(filtered)

about 4 years ago · Juan Pablo Isaza Relatório
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