-1

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 
iLemming
  • 34,477
  • 60
  • 195
  • 309
  • Please explain your problem in greater detail. What have you tried so far? What was the problem with your solution? Do you have a jsfiddle that can demonstrate the problem? – Xotic750 May 16 '13 at 22:18
  • Do you really wan t color to return "-1" or "1"? – Xotic750 May 16 '13 at 22:30
  • I'm sorry for the confusion... mixing grammar rules with math notation is bad – iLemming May 16 '13 at 22:34
  • This doe not appear arbitrary to me, the answers you are looking seem well defined, systematic even? – Xotic750 May 16 '13 at 22:40
  • Did you get an answer? Were any of the answers here of help? Do you want to share your answer, even give it as the answer to your own question, at least we can all benefit from the information. – Xotic750 Jun 12 '13 at 10:38

2 Answers2

2

Updated answer based on your further explanations

Javascript

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'}     
 ];

function getRank(data, number) {
    var ranks = {},
        rank = 0;

    data.forEach(function (entry) {
        if (typeof ranks[entry.title] !== "number") {
            ranks[entry.title] = rank;
            rank += 1;
        }
    });

    return ranks[data[number].title];
}


console.log(getRank(cars, 6));
console.log(getRank(cars, 0));
console.log(getRank(cars, 1));
console.log(getRank(cars, 3));

On jsfiddle

Output

2
0
0
1
Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • Arbitrary - "Based on random choice or personal whim, rather than any reason or system" – Xotic750 May 16 '13 at 22:16
  • you don't get it... "like this" does't mean exactly this particular array. I need a solution that works for any array of objects where every object has "title" property – iLemming May 16 '13 at 22:44
  • Correct, I didn't get your meaning as it was not clear from your question. Another update to your question may help. – Xotic750 May 16 '13 at 22:47
0

If it's already ordered (it must be cause otherwise I would really not understand your problem) you could do something like the following

var current_index = -1;
var current_title = "";

for (var i in arr) {
    if (arr[i].title!=current_title) {
        current_title = arr[i].title;
        current_index++;
    }
    arr[i].rank = current_index;

}
androidavid
  • 1,258
  • 1
  • 11
  • 20
  • Don't use for in to iterate an array. http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – Xotic750 May 16 '13 at 22:27
  • in this case it does not matter :) – androidavid May 17 '13 at 16:08
  • Perhaps not in this case, but even still you should consider safeguarding your loop with ´if (array.hasOwnProperty(i)) {...}´ See "for Statement": http://javascript.crockford.com/code.html – Xotic750 May 17 '13 at 17:38