javascript - 比较两个复杂的 json (Javascript)

标签 javascript arrays json

我正在尝试比较 js 中的两个 JSON 对象。我已经设法将我想要的信息检索到一个数组中,因为这两个 JSON 对象很复杂并且不必比较所有数据。但是,在我试图实现目标的 for 循环中,我不知道在哪里为第二个对象添加第二个循环,因为将它包含在第一个对象循环中会强制对象具有相同的长度,并且这不是真的。这是我到目前为止所拥有的:

    var modelsJSON = 
{
    "modelsARRAY":
                    [
                        {
                            "modelData": {
                                "manufacture": "Renault",
                                "name": "Clio",
                                "year": 2018
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": false,
                                "diesel": false,
                                "gasoline": true
                            }   
                        },
                        {
                           "modelData": {
                                "manufacture": "Renault",
                                "name": "Megane",
                                "year": 2019
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": true,
                                "diesel": false,
                                "gasoline": true
                            }   
                        },
                        {
                           "modelData": {
                                "manufacture": "Renault",
                                "name": "Laguna",
                                "year": 2019
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": true,
                                "diesel": true,
                                "gasoline": false
                            }   
                        }
                    ]
};


var clientsJSON = 
{
    "clientsARRAY":
                    [
                        {
                            "clientData": {
                                "name": "Peter",
                                "lastName": "McKay",
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": true,
                                "diesel": true,
                                "gasoline": true
                            }   
                        },
                        {
                           "clientData": {
                                "name": "John",
                                "lastName": "Lucas",
                            },
                            "modelSpecs": {
                                "manualGear": false,
                                "turbo": true,
                                "diesel": true,
                                "gasoline": false
                            }   
                        }
                    ]
};

var modelName = "";
var clientName = "";
var pdata = [];
var matches = 0;

for (var i in modelsJSON.modelsARRAY)
    {
        modelName += modelsJSON.modelsARRAY[i].modelData.name + " ";

        for (var j in modelsJSON.modelsARRAY[i].modelSpecs)
        {   
            pdata.push(modelsJSON.modelsARRAY[i].modelSpecs[j]);

        }

    }

    console.log(modelName, pdata);

我正在尝试实现这样的日志

"Client Peter has (3)matches with Clio car, (4)matches with Megane car and (x)matches with x car. \n Client John has (4)matches with Clio car, (2)matches with Megane car, and (y)matches with y car".

所以我可以用结果格式化一个新的 JSON。

最佳答案

您可以使用嵌套的 forEach 循环来获得更简洁的代码。此外,您可以将所有属性存储在一个数组中并循环遍历它,以便轻松比较对象属性。这是示例代码(我创建了一个自定义数据结构来存储结果):

var modelsJSON = 
{
    "modelsARRAY":
                    [
                        {
                            "modelData": {
                                "manufacture": "Renault",
                                "name": "Clio",
                                "year": 2018
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": false,
                                "diesel": false,
                                "gasoline": true
                            }   
                        },
                        {
                           "modelData": {
                                "manufacture": "Renault",
                                "name": "Megane",
                                "year": 2019
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": true,
                                "diesel": false,
                                "gasoline": true
                            }   
                        },
                        {
                           "modelData": {
                                "manufacture": "Renault",
                                "name": "Laguna",
                                "year": 2019
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": true,
                                "diesel": true,
                                "gasoline": false
                            }   
                        }
                    ]
};


var clientsJSON = 
{
    "clientsARRAY":
                    [
                        {
                            "clientData": {
                                "name": "Peter",
                                "lastName": "McKay",
                            },
                            "modelSpecs": {
                                "manualGear": true,
                                "turbo": true,
                                "diesel": true,
                                "gasoline": true
                            }   
                        },
                        {
                           "clientData": {
                                "name": "John",
                                "lastName": "Lucas",
                            },
                            "modelSpecs": {
                                "manualGear": false,
                                "turbo": true,
                                "diesel": true,
                                "gasoline": false
                            }   
                        }
                    ]
};

function Match(client, car, count) {
    this.client = client;
  this.car = car;
  this.count = count;
  this.toString = () => {return client + ' matches ' + car + ' for ' + count }
}
var matches = [];
clientsJSON.clientsARRAY.forEach(client => {
    modelsJSON.modelsARRAY.forEach(car => {
    var props = ["manualGear", "turbo", "diesel", "gasoline"];
    var count = 0;
    for (var prop of props) {
        if (client.modelSpecs[prop] === car.modelSpecs[prop]) {
        count++;
      }
    }
    matches.push(new Match(client.clientData.name, car.modelData.name, count));
  })
})

for (var match of matches) {
    console.log("" + match);
}

关于javascript - 比较两个复杂的 json (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54923312/

相关文章:

JavaScript:对象的重组和数组

c - 按字典顺序打印所有排列

php - 过滤后,检查过滤后数组是否不为空或是否具有所有原始值;像 Javascript 的 some() 和 every()

php - 如何在 PHP 中的 JSON 中添加属性?

javascript - HTML 注释标签中的 Javascript 是否仍应为 "hidden"?

javascript - react : Check if key is unique

javascript - 为什么元素的属性出现在内联函数的范围内?

javascript - 如何从外部站点访问和应用 JSON 数据?

python - 如何使用请求模块使用 Python 将 JSON 文件的内容发布到 RESTFUL API

javascript - this.functionName 的工作方式因事件监听器而异