比较两个复杂的json(Javascript)

时间:2019-02-28 10:20:49

标签: 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);

我正在尝试获得

这样的日志
  

“客户Peter与(克里奥)赛车有3场比赛,与”梅根“赛车有(4)场比赛   (x)与x汽车相匹配。 \ n客户John拥有(4)与克里奥汽车的比赛,(2)与梅根汽车的比赛,以及(y)与y汽车的比赛。”

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

1 个答案:

答案 0 :(得分:1)

您可以使用嵌套的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);
}