2013-08-05 81 views
1

所以看起來我在過去的幾個小時裏一直在撓撓我的腦袋......大概在6個小時左右,我似乎無法弄清楚。我已經看過各種問題/答案,但他們都沒有給我答案。JavaScript對象比較遞歸中斷

讓我從解釋這段代碼是SUPPOSE做什麼開始。 只要匹配所有object_layout,此代碼將匹配對象屬性與另一個對象。


注意:即使未提供完整的object_layout,我也希望對象匹配。

該數據對象:

var data = { 
    "some object" : { 
     name: "some object", 
     has: "properties", 
     types: [ 
      "some", 
      "type", 
      "of", 
      "array" 
     ] 
    }, 

    "another": { 
     property: false, 
     name: "another", 
     object: "here", 
     test: "this", 
     object: "strings" 
    }, 

    "minimal object": { 
     test: "this too" 
    }, 

    "minimal matching object": { 
     property: true, 
     name: "minimal matching object", 
     test: "this", 
     object: "strings" 
    }, 

    "matching object": { 
     test: "this", 
     property: true, 
     name: "matching object", 
     this_object: { 
      some: "object" 
    } 
    } 
}; 



甲typeof運算原型函數,可以檢測陣列。稍後會用到。

Object.prototype.typeof = function(object) { 
    if (!object) { return 'undefined' } 

    if (typeof(object) === "object" && 'splice' in object && 'join' in object) { 
     return 'array'; 
    } 

    return typeof(object); 
} 



查找功能是對象的原型。

Object.prototype.find = function(object_layout) { 

    var found_objects; 

    for (object in this) { // loop through objects in this object. 

    if (object != 'typeof' && object != 'find') { // skip these functions in our object. 
     console.log('object: ' + object); 

     for (property in object_layout) { 

     if (object_layout.hasOwnProperty(property)) { 

      var object_type = Object.typeof(object_layout[property]); 
      if (object_type == 'string') { 

      console.log('Property ' + property); 
      console.log('value: ' + object_layout[property]); 

      if (object_layout[property] != this[object][property]) { // if object_layout property doesnt exist in object. 
      if (found_objects && found_objects[object]) { console.log(object + " removed from found_objects"); delete found_objects[object]; }// if in found_objects then remove. 
       console.log("property doesn't exist."); 
       break; // break to next object. 
      } 

      if (!found_objects) { found_objects = {} } 
      if (!found_objects[object]) { console.log("Added object: " + object); found_objects[object] = this[object]; } 

      } else if (object_type == 'object') { // recurse into object 
      console.log('object type: ' + property); 
      console.log("Recurse: " + JSON.stringify(this[object][property])); 

      if (this[object][property]) { 
       this[object][property].find(object_layout[property]); // recurse broken... 
      } 

      break; // break to next object 
      } 

     } 
     } 
    } 
    } 

    if (found_objects) { return found_objects; } 
    return false; 
} 



函數調用:

var results = data.find(
{ 
    test: "this", 
    property: true, 
    this_object: { 
    some: "object" 
    } 
}; 

console.log(results), true, 3)); 



輸出日誌(修剪掉最後一位)

Added object: matching object 
Property property 
value: true 
object type: this_object 
Recurse: {"some":"object"} 
object: some 
Property some 
value: object 
property doesn't exist. 



一切似乎一直在工作,直到它遞歸時,然後以某種方式對象比較得到所有搞砸了,它不再匹配。

回答

0

好幾個小時後,我設法讓它工作,不得不說我對結果很滿意。現在,它:

  • 爲每個對象添加一個原型,以便您可以在任何地方使用它。只需要()它在你的應用程序中。
  • 會巧妙地跳過它不知道如何處理的任何對象。
  • 適用於字符串,數字,布爾和遞歸對象找到匹配。
  • 無限期遞增,以滿足您的需要!

我計劃在獲得更多時間時將數組對象添加到列表中。現在,這是代碼。我也計劃將這個模塊作爲Node用戶的npm模塊。也許甚至將它上傳到Github。敬請關注。

Object.prototype.find = function(object_layout) { 

    var found_objects; 

    for (object in this) { // loop through objects in this object. 
     var this_object = object; // place to store the current object. 

     if (Object.typeof([this[object]]) != 'function') { 

      for (property in object_layout) { 
       var this_property = property; // place to store the current property; 

       if (Object.typeof(this[this_object][this_property]) != 'function') { 

        if (object_layout.hasOwnProperty(this_property)) { 

         var object_type = Object.typeof(object_layout[this_property]); 
         if (object_type == 'string' || object_type == 'number' || object_type == 'boolean') { 

          if (object_layout[this_property] != this[this_object][this_property]) { // if object_layout property doesnt exist in object. 
           if (found_objects && found_objects[this_object]) { delete found_objects[this_object]; }// if in found_objects then remove. 
           break; // break to next object. 
          } 

          // Add object to found_objects 
          if (!found_objects) { found_objects = {} } 
          if (!found_objects[this_object]) { found_objects[this_object] = this[this_object]; } 

         } else if (object_type == 'object') { // recurse into object 

          if (!this[this_object][this_property]) { //object property doesn't exist 
           if (found_objects && found_objects[this_object]) { delete found_objects[this_object]; }// if in found_objects then remove. 
           break; 
          } 

          var recurse_object_data = {} 
          recurse_object_data[this_property] = this[this_object][this_property]; 

          if (!recurse_object_data.find(object_layout[this_property])) { // recursive property doesn't exist, delete it. 
           if (found_objects && found_objects[this_object]) { delete found_objects[this_object]; }// if in found_objects then remove. 
           break; // break to next object. 

          } else { 
           // Add object to found_objects 
           if (!found_objects) { found_objects = {} } 
           if (!found_objects[this_object]) { found_objects[this_object] = this[this_object]; } 
          } 

         } else if (object_type == 'array') { 


         } else { 
          //console.log('.find object: ' + object_type); 
         } 
        } 

       } 
      } 
     } 
    } 

    if (found_objects && found_objects.size() > 0) { return found_objects; } 
    return false; 
}