Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 3.24 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Backbone.js: compare model attributes in a collection
  2. modelOne:
  3.   color: "red"
  4.   age: 10
  5.   size: "large"
  6.  
  7. modelTwo:
  8.   color: "red"
  9.   age: 11
  10.   size: "medium"
  11.  
  12. modelThree:
  13.   color: "red"
  14.   age: 9
  15.   size: "large"
  16.        
  17. modelOne:
  18.   color: "red"
  19.   age: 10
  20.   sizes: ["small", "large"]
  21.  
  22. modelTwo:
  23.   color: "red"
  24.   age: 9
  25.   sizes: ["small", "large"]
  26.        
  27. var model = [
  28.     {
  29.       color: "red",
  30.       age: 10,
  31.       size: [ "small", "large" ]
  32.     },
  33.     {
  34.       color: "red",
  35.       age: 11,
  36.       size: [ "small", "large" ]
  37.     },
  38.     {
  39.       color: "red",
  40.       age: 9,
  41.       size: [ "small", "large" ]
  42.     }
  43. ];
  44.  
  45. function findCommonalities(data) {
  46.     if (data.length > 0) {
  47.         // It's safe enough to get the list of keys from the very first
  48.         // element. If the later ones differ, you know that the keys they
  49.         // had but the first element didn't are guaranteed not to be in
  50.         // the common set anyway because the first element didn't
  51.         // have them.
  52.         var keys = _.keys(data[0]);
  53.         var commonalities = { };
  54.  
  55.         _.each(keys,
  56.             function (key) {
  57.                 var values = _.pluck(data, key);
  58.  
  59.                 if (values.length == data.length) {
  60.                     // Sadly calling _.uniq() won't work when the values
  61.                     // plucked out are arrays themselves. It calls ===
  62.                     // and that's not sufficient for arrays.
  63.                     if (_.isArray(values[0])) {
  64.                         // However, we can get a little tricky here.
  65.                         // What if we _.zip() together all of the arrays
  66.                         // (assuming the ordering for each array is the
  67.                         // same) then we'll end up with an array of
  68.                         // arrays where each one can again be tested
  69.                         // with _.uniq() because each one will contain
  70.                         // the same value taken from each array.
  71.                         var zippedValues = _.zip(values);
  72.                         console.log("zippedValues", zippedValues);
  73.  
  74.                         if (!_.find(zippedValues,
  75.                             function (zippedValue) {
  76.                                 var uniqueValues = _.uniq(zippedValue);
  77.  
  78.                                 // Note: This test is the inverse of the
  79.                                 // one below. We're trying to find any
  80.                                 // array that has more than one value in
  81.                                 // it. If we do then there's some
  82.                                 // variance.
  83.                                 return uniqueValues.length != 1;
  84.                             })) {
  85.                             // We didn't find any arrays that had any
  86.                             // variance so we want this as well.
  87.                             commonalities[key] = values[0];
  88.                         }
  89.                     } else {
  90.                         var uniqueValues = _.uniq(values);
  91.  
  92.                         if (uniqueValues.length == 1) {
  93.                             commonalities[key] = uniqueValues[0];
  94.                         }
  95.                     }
  96.                 }
  97.             }
  98.         );
  99.  
  100.         return commonalities;
  101.     } else {
  102.         return { };
  103.     }
  104. }
  105.  
  106. console.log("commonalities: ", findCommonalities(model));