1

I'm trying to compare two JSON objects inside js. I've already manage to retrieve the info I desire into an array, since the two JSON objects are complex and not all the data has to be compared. However, in the for loop I'm trying to make for the goal, I don't know where to add the second loop for the second object, since including it inside the first object loop forces the objects to have the same length, and that's not true. Here's what I have so far:

    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);

I'm trying to achieve a log like

"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".

So I can format a new JSON with the result.

6
  • Do you know how to use array methods? Or do you expect solutions to use basic for loops? And do you have control over the format of this object? Since your structure can be made less complex easily, making the loops easier as well. Commented Feb 28, 2019 at 10:24
  • @Shilly I'm fairly new to Algorithms and js. I'm trying to figure that out by mself and googling, but I often get overcomplicated. I'm taking a look at array methods but how do apply them with a for loop? Commented Feb 28, 2019 at 10:31
  • Array methods are a replacement for for-loops. So .forEach() is a basic for loop. .map() is a for-loop that returns something for each element in the array. .reduce() is a loop that turns an array into anything else, like a different array or an object. .filter() turns an array into a different array with less elements. Et cetera. Commented Feb 28, 2019 at 10:37
  • 2
    So in your case, with a small bit of restructuring, you can have something like: clients.forEach( client => client.specs.map( spec => models.find( models.specs.some( spec )))); basically meaning: for each client, find all the models that have at least one spec that the client wants. You can obviously do the same with for loops, but the code will be way longer and in my opinion less readable, since you'll have to create alot of temporary variables to store everything as you search through all the models. Commented Feb 28, 2019 at 10:38
  • @Shilly Thanks! I'm going to rework the code a little bit with your hints and see what I can achieve. Commented Feb 28, 2019 at 10:54

1 Answer 1

1

You can use nested forEach loops for a cleaner code. Also, you can store all the properties in an array and loop through it for easily comparing the object properties. Here is the sample code (I have created a custom data structure for storing the results):

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.