1

I have an object containing arrays of objects. I'm trying to find the highest value of an object property, 'sortOrder' without manually iterating through the arrays and objects.

So my variable looks like this following:

const myObj = {
 people: [
   0: {firstname: 'Dave', lastName: 'Jones', sortOrder: 22},
   1: {firstname: 'Jane', lastName: 'Smith', sortOrder: 11}
 ],
 otherPeople: [
   0: {firstname: 'Jen', lastName: 'SomeLastName', sortOrder: 33},
   1: {firstname: 'ExampleFirstName', lastName: 'ExampleLastName', sortOrder: 12}
 ]
};

So I'd be trying to iterate through this to eventually find, in this case, the highest sortOrder of 33. Not necessarily the array index or the object containing it, just the number.

Thanks

2
  • Are you trying to have the people and otherPeople sorted by the sortOrder descending? Commented Dec 7, 2021 at 21:23
  • if you have them already sorted, then just return sortOrder of the first element in the array it will be the highest Commented Dec 7, 2021 at 21:30

4 Answers 4

1

const myObj = {
  people: [ {firstname: 'Dave', lastName: 'Jones', sortOrder: 22}, {firstname: 'Jane', lastName: 'Smith', sortOrder: 11} ],
  otherPeople: [ {firstname: 'Jen', lastName: 'SomeLastName', sortOrder: 33}, {firstname: 'ExampleFirstName', lastName: 'ExampleLastName', sortOrder: 12} ]
};

const maxSortOrder = 
  Object.values(myObj)
  .flat()
  .reduce((max, { sortOrder = 0 }) => sortOrder > max ? sortOrder : max, 0);

console.log(maxSortOrder);

Sign up to request clarification or add additional context in comments.

Comments

1

(I had to slightly correct your input data: arrays don't include the index number as you're showing.)

Easiest to combine the two arrays in the object into one list and then use reduce to find the maximum:

const myObj = {
  people: [
    {
      firstname: 'Dave',
      lastName: 'Jones',
      sortOrder: 22
    },
    {
      firstname: 'Jane',
      lastName: 'Smith',
      sortOrder: 11
    }
  ],
  otherPeople: [
    {
      firstname: 'Jen',
      lastName: 'SomeLastName',
      sortOrder: 33
    },
    {
      firstname: 'ExampleFirstName',
      lastName: 'ExampleLastName',
      sortOrder: 12
    }
  ]
};


let allPeople = [...myObj.people, ...myObj.otherPeople];

let max = allPeople.reduce((maxSort, person) => {
  return Math.max(maxSort, person.sortOrder)
}, 0));

console.log(max)

1 Comment

+1 for taking the clean your data/(re-)organize your data then it's simple approach. I too often fall victim to writing overly complex code to handle poor data choices, instead of fixing the root problem. (not that OP made a poor data choice in sample here, but data manipulation is a good solution for this problem anyway)
1

You can order them by the highest sortOrder and then take the higher of the highest elements in each array:

const myObj = {
  people: [{
      firstname: 'Dave',
      lastName: 'Jones',
      sortOrder: 22
    },
    {
      firstname: 'Jane',
      lastName: 'Smith',
      sortOrder: 11
    }
  ],
  otherPeople: [{
      firstname: 'Jen',
      lastName: 'SomeLastName',
      sortOrder: 33
    },
    {
      firstname: 'ExampleFirstName',
      lastName: 'ExampleLastName',
      sortOrder: 12
    }
  ]
};
Object.values(myObj).forEach(peopleArray => {
  peopleArray.sort((a, b) => b.sortOrder - a.sortOrder)
})
let maxValue = Math.max(myObj.people[0].sortOrder, myObj.otherPeople[0].sortOrder)
console.log(maxValue)

2 Comments

"If you have the array elements already sorted by sortOrder" I think your answer could be better if you take out the conditional and include the sorting as a necessary portion of the answer. As asked it isn't conditional / that's not extra information to the solution; its a distinct portion of it.
alright thank you for the tip
0
const myObj = {
people: [
    {firstname: 'Dave', lastName: 'Jones', sortOrder: 22},
    {firstname: 'Jane', lastName: 'Smith', sortOrder: 11}
],
otherPeople: [
    {firstname: 'Jen', lastName: 'SomeLastName', sortOrder: 33},
    {firstname: 'ExampleFirstName', lastName: 'ExampleLastName', sortOrder: 12}
]};

let maxSortOrder = 0;
for (const value of Object.values(myObj)) {
    maxSortOrder = value.reduce((acc, { sortOrder }) => sortOrder > acc ? sortOrder : acc, maxSortOrder);
};

console.log(maxSortOrder); //33

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.