Home > front end >  Js how to traverse the sequencing of the three biggest value after the map
Js how to traverse the sequencing of the three biggest value after the map

Time:10-07

Demand the following js page traverse map set out in the collection of the three biggest value for duplicate code how to write a great god show

CodePudding user response:

 
//give it a try, hand tapping, no test, should be the correct
The function getMax (list, num) {
Var result=[];
List. The sort (). The reverse () foreach ((p)=& gt; {
If (result. The lenth & lt; Num) {
If (result. IndexOf (p)!=1) {
Result. Push (p);
}
}
})
return result;
}

CodePudding user response:

The habitual knocked at the wrong place,,,, correct the following
 
//give it a try, hand tapping, no test, should be the correct
The function getMax (list, num) {
Var result=[];
List. The sort (). The reverse () foreach ((p)=& gt; {
If (result. The lenth & lt; Num) {
If (result. IndexOf (p)==1) {
Result. Push (p);
}
}
})
return result;
}

CodePudding user response:


 
//ok, it is wrong, this is a test
The function getMax (list, num) {
Var result=[];
Var arr=list. Sort (). The reverse ();
For (var I=0; I & lt; Arr. Length; I++) {
If (result. Length & lt; Num) {
If (result. IndexOf (arr) [I]==1) {
Result. Push (arr [I]);
}
}
}
return result;
}

CodePudding user response:

 
Var map=new map ();
The map. The set (" A ", 86);
The map. The set (" B ", 112);
The map. The set (" C ", 6);
The map. The set (" D ", 99);
The map. The set (" E ", 44).
The map. The set (" F ", 135);
Var arr=[... the map values ()]. Sort ((a, b)=& gt; B - a). Slice (0, 3);
The console. The log (arr);

CodePudding user response:

Need to go to a weight in the Set object
Var arr=[... new Set (map) values ())), sort ((a, b)=& gt; B - a). Slice (0, 3);
  • Related