Javascript对象比较递归中断

Javascript object comparison recursion broken

本文关键字:中断 递归 比较 对象 Javascript      更新时间:2023-09-26

所以在过去的几个小时里,我似乎一直在为此挠头。。大约6个小时了,我好像想不通。我看过SO上的各种问题/答案,但都没有给我答案。

让我首先解释一下这段代码应该做什么。只要与所有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" 
    }
    }
};



一种可以检测数组的原型函数。稍后将使用

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的原型

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.



一切似乎都在进行,直到它再次出现,然后不知何故,对象比较变得一团糟,不再匹配。

又过了几个小时,我终于成功了,不得不说我对结果很满意。目前,它:

  • 为每个对象添加一个原型,这样您就可以在任何地方使用它。只需在应用程序中要求()一次即可
  • 将巧妙地跳过它不知道如何处理的任何对象
  • 使用字符串、数字、布尔值和递归对象来查找匹配项
  • 无限期地重复,达到你需要的级别

我计划在获得更多时间后将数组对象添加到列表中。现在,这里是代码。我还计划为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;
}