Skip to content

Instantly share code, notes, and snippets.

@mixdev
Created September 6, 2019 17:32
Show Gist options
  • Save mixdev/3c47781df5e528ca104b4c92b63895c7 to your computer and use it in GitHub Desktop.
Save mixdev/3c47781df5e528ca104b4c92b63895c7 to your computer and use it in GitHub Desktop.
A reusable rank normalizer. Should use with map. (Reminder. you may use a normal loop counter if you have just one place to use this)
class rankNormalizer{
static normalize(){
rankNormalizer.count = typeof rankNormalizer.count !== 'undefined'?++rankNormalizer.count:1
//console.log(rankNormalizer.count);
return rankNormalizer.count;
};
static reset(){
rankNormalizer.count=null
}
}
rankNormalizer.normalize();// 1
rankNormalizer.normalize();// 2
rankNormalizer.normalize();// 3
rankNormalizer.reset();
rankNormalizer.normalize();// 1
rankNormalizer.normalize();// 2
rankNormalizer.normalize();// 3
@mixdev
Copy link
Author

mixdev commented Sep 7, 2019

However, you can do this in oneshot if you use sort() & keys() on an array. If it is an object array, use Lodash but use the same technique

x=[{ 'a': 1, 'b': 2 , c:3, d:4, e:5},{ 'a': 2, 'b': 3 , c:3, d:4, e:5},{ 'a': 6, 'b': 3 , c:3, d:4, e:5}]   y=_.keys(x);//it is array of strings: you may need to typecast in some cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment