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

147
Visualizações
groupby and select max id from object in react native

How to groupby and select max id from object in react native. that was just a dummy data to explain you that how my object look like

[
 {"name": "alex", "subject": "english" "student_id": "1"},
 {"name": "hales", "subject": "science" "student_id": "2"},
 {"name": "joss", "subject": "english" "student_id": "3"},
 {"name": "alexandra", "subject": "science" "student_id": "4"},
 {"name": "mark", "subject": "math" "student_id": "5"},
]

First of all I want to group by subject and then select the student with max id so my output should look like that

[
 {"name": "joss", "subject": "english" "student_id": "3"},
 {"name": "alexandra", "subject": "science" "student_id": "4"},
 {"name": "mark", "subject": "math" "student_id": "5"},
]

What I have tried So far is that

const result = myobject.reduce(function (r, a) {
      r[a.case_id] = r[a.case_id] || [];
      r[a.case_id].push(a);
      return r;
    }, Object.create(null));

with above code I can group by but not able to get the max id so how could I achieve that?

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

0

You could group by subject and replace the value if student_id is greater.

As result take the values from the object.

const
    data = [{ name: "alex", subject: "english", student_id: "1" }, { name: "hales", subject: "science", student_id: "2" }, { name: "joss", subject: "english", student_id: "3" }, { name: "alexandra", subject: "science", student_id: "4" }, { name: "mark", subject: "math", student_id: "5" }],
    result = Object.values(data.reduce((r, o) => {
        if (!r[o.subject] || +r[o.subject].student_id < +o.student_id) {
            r[o.subject] = o;
        }
        return r;
    }, Object.create(null)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

lodash if don't mind. It seems like a clean and simple way to understand

const data = [{ name: "alex", subject: "english", student_id: "1" }, { name: "hales", subject: "science", student_id: "2" }, { name: "joss", subject: "english", student_id: "3" }, { name: "alexandra", subject: "science", student_id: "4" }, { name: "mark", subject: "math", student_id: "5" }];

const result = _.chain(data)
  .groupBy('subject')
  .values()
  .map((group) => _.maxBy(group, 'student_id'))
  .value();
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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