const arr = [
{ val: 1, from: 0, to: 4 },
{ val: 2, from: 0, to: 6 },
{ val: 3, from: 3, to: 8 },
{ val: 4, from: 3, to: 10 },
{ val: 5, from: 6, to: 15 }];
const result = [2, 4, 5] // val
val
с наибольшим to
и одинаковим from
const arr = [
{ val: 1, from: 0, to: 4 },
{ val: 2, from: 0, to: 6 },
{ val: 3, from: 3, to: 8 },
{ val: 4, from: 3, to: 10 },
{ val: 5, from: 6, to: 15 }];
const result = arr.reduce((acc,v)=>{
if(!acc.dict[v.from]){
acc.dict[v.from] = {idx:acc.cur, to:v.to};
acc.arr[acc.cur] = v.val;
acc.cur += 1;
}else{
const info = acc.dict[v.from];
if(v.to>info.to){
acc.arr[info.idx] = v.val;
}
}
return acc;
}, {
dict:{},
arr:[],
cur: 0,
}).arr;
const values = Object.values(arr.reduce((acc, n) => {
const t = acc[n.from];
acc[n.from] = (t && t.to > n.to) ? t : n;
return acc;
}, {})).map(n => n.val);
const values = Object.values(arr.reduce((acc, n) => {
(acc[n.from] = acc[n.from] || [])[n.to] = n.val;
return acc;
}, {})).map(n => n.pop());
const values = [...arr]
.sort((a, b) => (a.from - b.from) || (a.to - b.to))
.filter((n, i, a) => !a[i + 1] || a[i + 1].from !== n.from)
.map(n => n.val);
Можно с помощью lodash.
const values = _
.chain(arr)
.groupBy('from')
.map(n => _.maxBy(n, m => m.to).val)
.value();