简体   繁体   English

Javascript - 在对象的 arrays 的 object 中找到最高值

[英]Javascript - find highest value in an object of arrays of objects

I have an object containing arrays of objects.我有一个包含 arrays 对象的 object。 I'm trying to find the highest value of an object property, 'sortOrder' without manually iterating through the arrays and objects.我试图找到 object 属性“sortOrder”的最大值,而无需手动遍历 arrays 和对象。

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.所以我会尝试遍历这个最终找到,在这种情况下,最高的 sortOrder 33。不一定是数组索引或包含它的 object,只是数字。

Thanks谢谢

 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).reduce((max, arr) => { arr.forEach(({ sortOrder }) => { if(sortOrder > max) { max = sortOrder; } }); return max; }, 0); console.log(maxSortOrder);

(I had to slightly correct your input data: arrays don't include the index number as you're showing.) (我不得不稍微更正您的输入数据:arrays 不包括您显示的索引号。)

Easiest to combine the two arrays in the object into one list and then use reduce to find the maximum:最简单的方法是将 object 中的两个 arrays 合并为一个列表,然后使用reduce找到最大值:

 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)

You can order them by the highest sortOrder and then take the higher of the highest elements in each array:您可以按最高的 sortOrder 对它们进行排序,然后在每个数组中取最高元素中的较高者:

 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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM