Array.prototype.orderby=function() { let self=this; let arr=[self[0]], i=1; for(;i<self.length; i++) { let index=arr.findIndex(f=>f > self[i]); arr.splice(index==-1?arr.length: index, 0, self[i]); } return arr; } Array.prototype.orderdescby=function() { let self=this; let arr=[self[0]], i=1; for(;i<self.length; i++) { let index=arr.findIndex(f=>f < self[i]); arr.splice(index==-1?arr.length:index,0,self[i]); } return arr; } const points = [40, 100, 1, 500, 25, 107,0]; console.log(points.orderby()) console.log(points.orderdescby())
Thursday, July 18, 2024
Using Orderby ascending and Orderby descending to sort array javascript
Tags
Array
,
ascending
,
descending
,
JavaScript
,
orderby
,
sort
Wednesday, July 17, 2024
Javascript String extend for new function.
Javascript String extend for new function.
First Approch:
Second Approch:
First Approch:
String.prototype.toArray=function(){ let arr=[]; for(let ch of this){ arr.push(ch); } return arr; } let name="suresh"; console.log(name.toArray()); //output [ 's', 'u', 'r', 'e', 's', 'h' ]
Second Approch:
String.prototype.toArray=function(){ return this.split(''); } let name="suresh"; console.log(name.toArray()); //output [ 's', 'u', 'r', 'e', 's', 'h' ]
Subscribe to:
Posts
(
Atom
)