I needed to write a quick function to compare two arrays and report
the differences(index matters), in my case the old order of a sortable
container to the new order onUpdate. May come in handy for the next
guy..

mya1 = ['1','2','3','4','5'];
mya2 = ['1','2','3','40','5'];
mya3 = ['1','20','3','4','50'];

function findDelta(a1,a2){
  return a1.inject([], function(array, value, index) {
    if (value != a2[index])
      array.push({value:value, index:index, delta:a2[index]});
  return array;
 })
}

findDelta(mya1,mya2).toJSON();
//returns [{"value": "4", "index": 3, "delta": "40"}]

findDelta(mya1,mya3).toJSON();
//returns [{"value": "2", "index": 1, "delta": "20"}, {"value": "5",
"index": 4, "delta": "50"}]

findDelta(mya2,mya3).toJSON();
//returns [{"value": "2", "index": 1, "delta": "20"}, {"value": "40",
"index": 3, "delta": "4"}, {"value": "5", "index": 4, "delta": "50"}]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to