If I have an array like this
var cars=
[
{ 'title':'brand', 'value':'honda'}
{ 'title':'brand', 'value':'toyota'}
{ 'title':'color', 'value':'red'}
{ 'title':'color', 'value':'white'}
{ 'title':'year', 'value':'1995'}
{ 'title':'year', 'value':'2006'}
{ 'title':'year', 'value':'2007'}
]
How can I write a single function that would return element's rank. So for this array, for elements with title "brand" it should return 0, for elements with title "color" should return 1 and so on. It should not cache or use any mapping table but should determine the rank on flight, so any time you call
getRank(cars[6]) == 2 //true for the last element
getRank(cars[0]) == 0 //true for the first element
getRank(cars[1]) == 0 //true for the second element
getRank(cars[3]) == 1 //true for the fourth element