如何使用reduce从具有对象数组的对象数组中找到最大值
How to use reduce to find maximum from array of objects with array of objects
如果我有以下
arr = [
{key: "a",
values : [{key: "aa", value: 2}, {key: "bb", value: 5}]},
{key: "b",
values : [{key: "cc", value: 7}, {key: "dd", value: 3}]}
]
如何在javascript中使用reduce
从嵌套对象中找到最大值?在上述情况下答案应该是 7。
我目前可以使用循环来实现:
let max = 0;
let findDataMax = function(d) {
for (let i = 0; i < d.length; i++) {
let currArr = d[i].values;
let tempArr = []
currArr.forEach((d) => tempArr.push(+d.value));
if (Math.max(...tempArr) > max) {
max = Math.max(...tempArr);
}
}
}
let arr = [
{key: "a", values : [{key: "aa", value: 2}, {key: "bb", value: 5}]},
{key: "b",values : [{key: "cc", value: 7}, {key: "dd", value: 3}]}
];
findDataMax(arr);
console.log(max);
我更愿意为此使用 reduce 以外的其他方法,但如果必须这样做,那么您可以将累加器设置为 -Infinity
开始(这样与累加器相比的任何值都将是大于 -Infinity
)。对于数组中的每个对象,您可以通过将 values
的数组映射到每个对象的 value
个数字数组,然后 spreading these numbers into a call to Math.max()
来找到最大值。然后,您可以比较这是否大于当前最大值,如果是,则 return 作为新值用作累加器,否则,使用旧累加器值:
const arr = [ {key: "a", values : [{ key: "aa", value: 2}, { key: "bb",value: 5}]}, {key: "b", values : [{ key: "cc", value: 7}, { key: "dd", value: 3}]} ];
const max = arr.reduce((max, {values}) => {
const newMax = Math.max(...values.map(({value}) => value));
return newMax > max ? newMax : max;
}, -Infinity);
console.log(max);
如前所述,我可能会使用不同的方法来 .reduce()
,例如 .flatMap()
来获取所有对象 value
数字,然后您可以将其传播到对Math.max()
:
const arr = [ {key: "a", values : [{ key: "aa", value: 2}, { key: "bb",value: 5}]}, {key: "b", values : [{ key: "cc", value: 7}, { key: "dd", value: 3}]} ];
const max = Math.max(...arr.flatMap(({values}) => values.map(({value}) => value)));
console.log(max);
我不知道 reduce
函数的使用是否是解决这个问题的一个干净的解决方案,但这里有它:
const arr = [{ key: 'a', values: [{ key: 'aa', value: 2 }, { key: 'bb', value: 5 }] }, { key: 'b', values: [{ key: 'cc', value: 7 }, { key: 'dd', value: 3 }] }];
// O(n * b)
const maxValue = arr.reduce((prev, item) => item
.values.reduce((subPrev, subItem) => (subItem.value > subPrev ? subItem.value : subPrev), prev), 0);
console.log(maxValue); // 7
如果我有以下
arr = [
{key: "a",
values : [{key: "aa", value: 2}, {key: "bb", value: 5}]},
{key: "b",
values : [{key: "cc", value: 7}, {key: "dd", value: 3}]}
]
如何在javascript中使用reduce
从嵌套对象中找到最大值?在上述情况下答案应该是 7。
我目前可以使用循环来实现:
let max = 0;
let findDataMax = function(d) {
for (let i = 0; i < d.length; i++) {
let currArr = d[i].values;
let tempArr = []
currArr.forEach((d) => tempArr.push(+d.value));
if (Math.max(...tempArr) > max) {
max = Math.max(...tempArr);
}
}
}
let arr = [
{key: "a", values : [{key: "aa", value: 2}, {key: "bb", value: 5}]},
{key: "b",values : [{key: "cc", value: 7}, {key: "dd", value: 3}]}
];
findDataMax(arr);
console.log(max);
我更愿意为此使用 reduce 以外的其他方法,但如果必须这样做,那么您可以将累加器设置为 -Infinity
开始(这样与累加器相比的任何值都将是大于 -Infinity
)。对于数组中的每个对象,您可以通过将 values
的数组映射到每个对象的 value
个数字数组,然后 spreading these numbers into a call to Math.max()
来找到最大值。然后,您可以比较这是否大于当前最大值,如果是,则 return 作为新值用作累加器,否则,使用旧累加器值:
const arr = [ {key: "a", values : [{ key: "aa", value: 2}, { key: "bb",value: 5}]}, {key: "b", values : [{ key: "cc", value: 7}, { key: "dd", value: 3}]} ];
const max = arr.reduce((max, {values}) => {
const newMax = Math.max(...values.map(({value}) => value));
return newMax > max ? newMax : max;
}, -Infinity);
console.log(max);
如前所述,我可能会使用不同的方法来 .reduce()
,例如 .flatMap()
来获取所有对象 value
数字,然后您可以将其传播到对Math.max()
:
const arr = [ {key: "a", values : [{ key: "aa", value: 2}, { key: "bb",value: 5}]}, {key: "b", values : [{ key: "cc", value: 7}, { key: "dd", value: 3}]} ];
const max = Math.max(...arr.flatMap(({values}) => values.map(({value}) => value)));
console.log(max);
我不知道 reduce
函数的使用是否是解决这个问题的一个干净的解决方案,但这里有它:
const arr = [{ key: 'a', values: [{ key: 'aa', value: 2 }, { key: 'bb', value: 5 }] }, { key: 'b', values: [{ key: 'cc', value: 7 }, { key: 'dd', value: 3 }] }];
// O(n * b)
const maxValue = arr.reduce((prev, item) => item
.values.reduce((subPrev, subItem) => (subItem.value > subPrev ? subItem.value : subPrev), prev), 0);
console.log(maxValue); // 7