JavaScript Return object(s) in array with highest property value per group return objects array,return array objects php,return array objects

I have an array containing several objects similar to the following:

{person: {name: "Steve", id: 1}, role: 1}
{person: {name: "Phil", id: 2}, role: 1}
{person: {name: "Steve", id: 1}, role: 3}
{person: {name: "Phil", id: 2}, role: 6}

My intention is to return an array of the same type, but I'd like to return only one object per "person" with their highest role.

I understand the following will give me a single object with the highest role.

array.reduce((prev, cur) => prev.role > cur.role ? prev : cur);

How do I return each unique person and their corresponding highest role as a new array?

Like so:

{person: {name: "Steve", id: 1}, role: 3}
{person: {name: "Phil", id: 2}, role: 6}
Answer:1

You need to collect the objects and if you have already one with the same id check the role and take the greater one.

var data = [{ person: { name: "Steve", id: 1 }, role: 1 }, { person: { name: "Phil", id: 2 }, role: 1 }, { person: { name: "Steve", id: 1 }, role: 3 }, { person: { name: "Phil", id: 2 }, role: 6 }],
    grouped = data.reduce((r, o) => {
        var index = r.findIndex(({ person: { id } }) => id === o.person.id);
        if (index === -1) {
            r.push(o);
            return r;
        }
        if (r[index].role < o.role) {
            r[index] = o;
        }
        return r;    
    }, []);

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

Group the items by person.id with Array.reduce(), but for each id store only the object with the highest role. Convert back to array using Object.values():

const data = [{ person: { name: "Steve", id: 1 }, role: 1 }, { person: { name: "Phil", id: 2 }, role: 1 }, { person: { name: "Steve", id: 1 }, role: 3 }, { person: { name: "Phil", id: 2 }, role: 6 }];

const result = Object.values(data.reduce((r, o) => {
  const id = o.person.id;
  
  if(!r[id] || r[id].role < o.role) r[id] = o;
  
  return r;
}, []));

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

I want to pass the value from select list - ListComponentComponent to sibling component - DisplayComponentComponent and display the value in the template of DisplayComponentComponent. I want to use ...

I want to pass the value from select list - ListComponentComponent to sibling component - DisplayComponentComponent and display the value in the template of DisplayComponentComponent. I want to use ...

import React, { useContext } from 'react'; useContext is undefined. error details: Uncaught (in promise) TypeError: Object(...) is not a function error when processing const context = ...

import React, { useContext } from 'react'; useContext is undefined. error details: Uncaught (in promise) TypeError: Object(...) is not a function error when processing const context = ...

  1. react typeerror object(...) is not a function
  2. react typeerror object.values is not a function
  3. react typeerror object(...)(...).then is not a function
  4. react typeerror object expected
  5. react typeerror object(...) is not a function hooks
  6. react typeerror object(...)(...) is undefined
  7. reactjs typeerror object(...) is not a function
  8. react uncaught typeerror object(...) is not a function
  9. react usestate typeerror object(...) is not a function
  10. react redux typeerror object(...) is not a function
  11. react bootstrap typeerror object(...) is not a function
  12. react lazy typeerror object(...) is not a function
  13. react useeffect typeerror object(...) is not a function
  14. react dropzone typeerror object(...) is not a function
  15. react makestyles typeerror object(...) is not a function
  16. react × typeerror object(...) is not a function
  17. react hoc typeerror object(...) is not a function
  18. react typeerror cyclic object value
  19. react error typeerror object(...) is not a function
  20. react useref typeerror object(...) is not a function

var imagecount = 1; var total = 3; I'd like to know why I can't make an imageslider this way. I have pictures saved such as studentbild1, studentbild2 and studentbild3. How do I get those to show in ...

var imagecount = 1; var total = 3; I'd like to know why I can't make an imageslider this way. I have pictures saved such as studentbild1, studentbild2 and studentbild3. How do I get those to show in ...

I'm in the process of making a code that prints all numbers divisable by 3 using an array, but im having trouble removing specific values. All the values i want prints correctly, but what i want is to ...

I'm in the process of making a code that prints all numbers divisable by 3 using an array, but im having trouble removing specific values. All the values i want prints correctly, but what i want is to ...

  1. remove specific values from list python
  2. remove specific values in r
  3. remove specific values from numpy array
  4. remove specific values from vector r
  5. remove specific values from array javascript
  6. remove specific values from array php
  7. remove specific values from list
  8. remove specific values from array matlab
  9. remove specific values from data frame r
  10. remove specific values from column excel
  11. remove specific values from array python
  12. remove specific values from matrix matlab
  13. remove specific values in excel
  14. remove specific values from array
  15. remove specific values from array js
  16. remove specific values from vector matlab
  17. remove specific values from dataframe
  18. remove specific values from list excel
  19. remove specific values from list java
  20. remove specific values