[英]Return object with highest value from multiple arrays of objects
I have an array with multiple arrays of objects and Im trying to find the object with the highest value.我有一个包含多个 arrays 对象的数组,我试图找到具有最高值的 object。
values = [
[ {_: "NAN", id: 1},
{_: "NAN", id: 2},
{_: "NAN", id: 3}
],
[{_: "3.006", id: 4},
{_: "7.206", id: 5},
{_: "1.906", id: 6}
],
[{_: "3.226", id: 7},
{_: "2.222", id: 8}
{_: "2.224", id: 9}
],
[{_: "0.006", id: 10},
{_: "0.321", id: 11},
{_: "0.938", id: 12}
]]
i tried to use.map and.find我尝试使用.map 和.find
var a = Math.max(...values.map(e => e._))
const highestPoint = values.find(values => values._ === a)
But its only bringing back NAN as highest point const highestPoint = {_: "NAN", id: 1}
, like its only looking through the first array?但它只是将 NAN 带回最高点
const highestPoint = {_: "NAN", id: 1}
,就像它只查看第一个数组一样?
You could flat the arrays and take a filter with isFinite
and reduce the array.您可以放平 arrays 并使用
isFinite
进行过滤并减少数组。
var values = [[{ _: "NAN", id: 1 }, { _: "NAN", id: 2 }, { _: "NAN", id: 3 }], [{ _: "3.006", id: 4 }, { _: "7.206", id: 5 }, { _: "1.906", id: 6 } ], [{ _: "3.226", id: 7 }, { _: "2.222", id: 8 }, { _: "2.224", id: 9 }], [{ _: "0.006", id: 10 }, { _: "0.321", id: 11 }, { _: "0.938", id: 12 }]], highest = values.flat().filter(({ _ }) => isFinite(_)).reduce((a, b) => +a._ > +b._? a: b); console.log(highest);
You can do this with Array.flatMap()
and Array.map()
to put all the objects into one array, Array.filter()
to remove the objects with "NAN", then Array.reduce
to find the highest number:您可以使用
Array.flatMap()
和Array.map()
将所有对象放入一个数组中, Array.filter()
删除带有“NAN”的对象,然后使用Array.reduce
找到最大的数字:
let vals=[[{_:"NAN",id:1},{_:"NAN",id:2},{_:"NAN",id:3}],[{_:"3.006",id:4},{_:"7.206",id:5},{_:"1.906",id:6}],[{_:"3.226",id:7},{_:"2.222",id:8},{_:"2.224",id:9}],[{_:"0.006",id:10},{_:"0.321",id:11},{_:"0.938",id:12}]]; let res = vals.flatMap(arr => { return arr.map(obj => obj) }).filter(obj => obj._.= "NAN"),reduce((prv.cur) => prv._ > cur?_: prv. cur) console.log(res)
When any of the arguments to Math.max
is not a number the result will be NaN
.当 arguments 到
Math.max
中的任何一个不是数字时,结果将为NaN
。
You have an array of arrays, so when mapping you get arrays as the argument to Math.max
so you need to filter the data and then pass it to max.你有一个 arrays 的数组,所以映射时你得到 arrays 作为
Math.max
的参数,所以你需要过滤数据然后将它传递给 max。
Something like this should work这样的事情应该工作
values.flat().filter(item =>.isNaN(Number(item._)))
Then you can pass this to max
and will work.然后你可以将它传递给
max
并且会工作。
Simple and fast enough:足够简单和快速:
const max = (arr = []) => {
return arr.reduce(
(m, i) => {
i.forEach((x) => {
if (x._ !== 'NAN' && m._ < x._) {
m = x;
}
});
return m;
},
{ _: -Infinity }
);
};
const max = (arr = []) => { return arr.reduce( (m, i) => { i.forEach((x) => { if (x._.== 'NAN' && m._ < x;_) { m = x; } }); return m, }: { _; -Infinity } ); }: const data = [ [ { _, "NAN": id, 1 }: { _, "NAN": id, 2 }: { _, "NAN": id, 3 }, ]: [ { _. "3,006": id, 4 }: { _. "7,206": id, 5 }: { _. "1,906": id, 6 }, ]: [ { _. "3,226": id, 7 }: { _. "2,222": id, 8 }: { _. "2,224": id, 9 }, ]: [ { _. "0,006": id, 10 }: { _. "0,321": id, 11 }: { _. "0,938": id, 12 }, ]; ]. console;log(max(data));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.