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

204
Visualizações
How to get unique data from array of object by id in javascript

Hello all I need help I am trying to get the unique data from an array of objects by id in JavaScript I am not able to fetch it below is my array

[
  {id: 408, customer_id: 2, bill_no: 381, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 409, customer_id: 3, bill_no: 382, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 410, customer_id: 4, bill_no: 383, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 411, customer_id: 6, bill_no: 384, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 412, customer_id: 7, bill_no: 385, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 413, customer_id: 8, bill_no: 386, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 414, customer_id: 9, bill_no: 387, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 387, customer_id: 2, bill_no: 360, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 388, customer_id: 3, bill_no: 361, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 389, customer_id: 4, bill_no: 362, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 390, customer_id: 6, bill_no: 363, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 391, customer_id: 7, bill_no: 364, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 392, customer_id: 8, bill_no: 365, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 393, customer_id: 9, bill_no: 366, bill_period: 'weekly', from_date: '2021-10-03'}
  {id: 380, customer_id: 2, bill_no: 353, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 381, customer_id: 3, bill_no: 354, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 382, customer_id: 4, bill_no: 355, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 383, customer_id: 6, bill_no: 356, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 384, customer_id: 7, bill_no: 357, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 385, customer_id: 8, bill_no: 358, bill_period: 'weekly', from_date: '2021-09-26'}
  {id: 386, customer_id: 9, bill_no: 359, bill_period: 'weekly', from_date: '2021-09-26'}
]

I want the unique data from the above array by customer_id with their latest id I want the result like the below code

[
  {id: 408, customer_id: 2, bill_no: 381, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 409, customer_id: 3, bill_no: 382, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 410, customer_id: 4, bill_no: 383, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 411, customer_id: 6, bill_no: 384, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 412, customer_id: 7, bill_no: 385, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 413, customer_id: 8, bill_no: 386, bill_period: 'weekly', from_date: '2021-10-10'}
  {id: 414, customer_id: 9, bill_no: 387, bill_period: 'weekly', from_date: '2021-10-10'}
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is no pretty ready made function, you need to roll your own; but it should be pretty simple to do. An excellent use case for a reducer!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

const arr = [
  {id: 408, customer_id: 2, bill_no: 381, bill_period: 'weekly', from_date: '2021-10-10'},
  {id: 409, customer_id: 3, bill_no: 382, bill_period: 'weekly', from_date: '2021-10-10'},
  {id: 410, customer_id: 4, bill_no: 383, bill_period: 'weekly', from_date: '2021-10-10'},
  {id: 411, customer_id: 6, bill_no: 384, bill_period: 'weekly', from_date: '2021-10-10'},
  {id: 412, customer_id: 7, bill_no: 385, bill_period: 'weekly', from_date: '2021-10-10'},
  {id: 413, customer_id: 8, bill_no: 386, bill_period: 'weekly', from_date: '2021-10-10'},
  {id: 414, customer_id: 9, bill_no: 387, bill_period: 'weekly', from_date: '2021-10-10'},
  {id: 387, customer_id: 2, bill_no: 360, bill_period: 'weekly', from_date: '2021-10-03'},
  {id: 388, customer_id: 3, bill_no: 361, bill_period: 'weekly', from_date: '2021-10-03'},
  {id: 389, customer_id: 4, bill_no: 362, bill_period: 'weekly', from_date: '2021-10-03'},
  {id: 390, customer_id: 6, bill_no: 363, bill_period: 'weekly', from_date: '2021-10-03'},
  {id: 391, customer_id: 7, bill_no: 364, bill_period: 'weekly', from_date: '2021-10-03'},
  {id: 392, customer_id: 8, bill_no: 365, bill_period: 'weekly', from_date: '2021-10-03'},
  {id: 393, customer_id: 9, bill_no: 366, bill_period: 'weekly', from_date: '2021-10-03'},
  {id: 380, customer_id: 2, bill_no: 353, bill_period: 'weekly', from_date: '2021-09-26'},
  {id: 381, customer_id: 3, bill_no: 354, bill_period: 'weekly', from_date: '2021-09-26'},
  {id: 382, customer_id: 4, bill_no: 355, bill_period: 'weekly', from_date: '2021-09-26'},
  {id: 383, customer_id: 6, bill_no: 356, bill_period: 'weekly', from_date: '2021-09-26'},
  {id: 384, customer_id: 7, bill_no: 357, bill_period: 'weekly', from_date: '2021-09-26'},
  {id: 385, customer_id: 8, bill_no: 358, bill_period: 'weekly', from_date: '2021-09-26'},
  {id: 386, customer_id: 9, bill_no: 359, bill_period: 'weekly', from_date: '2021-09-26'}
];

// reduce array to only relevant values to a single variable
const filtered = arr.reduce((accumulator, current) => {
  // If customer data is already added to accumulator, don't add
  if (accumulator.find(x => x.customer_id ===  current.customer_id)) {
    return accumulator;
  }

  accumulator.push(current);
  return accumulator;
}, []);

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