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

128
Visualizações
Use 2 filter in array java script

I have an array like this

I would like to bring only the installments with checked = true and with payments checked = true

I have this array

 0: {
    checked: true,
    code: 100.
    paymant: Array(2)
    {
        0: {data: 03/12/2021, checked: true},
        1: {data: 03/12/2021}
    }
 },
  1: {
    code: 100.
    paymant: Array(2)
    {
        0: {data: 03/12/2021},
        1: {data: 03/12/2021}
    }
 }

// Real Data
const data = [
    {
        checked: true,
        code: 100,
        payments: [
            {data: 'checked', checked: true},
            {data: 'not-checked'}
        ]
    },
    {
        code: 100,
        payments: [
            {data: 'not-checked'},
            {data: 'not-checked'}
        ]
    }
];

I would like this

0: {
    checked: true,
    code: 100.
    paymant: Array(1)
    {
        0: {data: 03/12/2021, checked: true},
    }
 },

I try to do this but no work

 installments.filter(i => i.checked).filter(i => i.paymant.filter(p => p.checked))
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The first filter is straight forward but you'll need to rebuild the payments array so it only contains the checked entry.

const data = [
    {
        checked: true,
        code: 100,
        payments: [
            {data: 'checked', checked: true},
            {data: 'not-checked'}
        ]
    },
    {
        code: 100,
        payments: [
            {data: 'not-checked'},
            {data: 'not-checked'}
        ]
    }
];

const checked = (item) => item && item.checked;
const paymentCheckedMapper = (item) => (
    {...item, payments: item.payments.filter(checked)}
);

data.filter(checked).map(paymentCheckedMapper)

https://jsfiddle.net/vebwfnL0/

about 4 years ago · Juan Pablo Isaza Relatório

0

Since you need to mix and match filter and map. You can use reduce here. No need to filter then map, It will cost performance issues.

const data = [
  {
    checked: true,
    code: 100,
    paymant: [
      { data: 03 / 12 / 2021, checked: true },
      { data: 03 / 12 / 2021 },
    ],
  },
  {
    code: 101,
    paymant: [{ data: 03 / 12 / 2021 }, { data: 03 / 12 / 2021 }],
  },
];

const getPaid = (records = []) => {
  return records.reduce((acc, record) => {
    if (!record.checked) return acc;
    const paymant = record.paymant.filter((paymant) => paymant.checked);
    if (paymant.length) {
      acc.push({ ...record, paymant });
    }
    return acc;
  }, []);
};
console.log(getPaid(data));

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