我如何将这两个函数组合在一起,使我有一个约简

问题描述 投票:0回答:1
第一个函数将所有GS应用于特定视图,第二个函数返回最高度。我怎样才能同时使用一个减少?

private getMap(): Array<ViewG> { const gW = ViewManager.getView().getGW(); return gW.reduce((currentG, w) => { const GS = this.query.getAppliedGW(w); currentG.push([{ GS, priority:appliedGP[GS] }]); return currentG; }, []); } public getHighest(): ViewG { const entries = [...this.getMap()]; if (entries.length !== 0) { const highestG = entries.reduce((prev, current) => prev.priority > current.priority ? prev : current )[0]; return highestG; }
javascript typescript
1个回答
0
投票
public getHighest(): ViewG { //1. Take all GWs like in getMap() const gW = ViewManager.getView().getGW(); //2. call .reduce() again to go over them return gW.reduce((prev, w) => { //3. transform to a GS const GS = this.query.getAppliedGW(w); //4. instead of populating an array, just create the element const current = { GS, priority: appliedGP[GS] }; //5. extract the previous priority in a null safe way: const prevPriority = prev?.priority ?? -Infinity; // ^ ^^ ^^^^^^^^^ // | || | // optional chaining ----- || | // nullish coalescing operator ------- | // fallback value in the case of null ------ //5. compare with the previous one and return the higher priority return prev.priority > current.priority ? prev : current; }, null ); // ^^^^ the default value }
© www.soinside.com 2019 - 2024. All rights reserved.