You could add a member method that accepts another car object as an argument and returns true if the cars have the same value for given member attribute.
Ex.
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
this.isSameMake = function(otherCar){
return this.make === otherCar.make;
}
this.printCar = function () { // added this method to easily print all the data about car
return "Make: " + this.make + "<br>" +
"Model: " + this.model + "<br>" +
"Year: " + this.year + "<br>" +
"Owner: " + this.owner + "<hr>";
}
}
Also i think it would be smarter to add those car objects to an array so you can easily iterate through all of them and compare their values with a for loop.
var allCars = [];
allCars.push(new Car('eagle', 'Talon TSi', 1993, rand));
allCars.push(new Car('nissan', '300ZX', 1992, ken));
allCars.push(new Car('nissan', '54353', 2001, barbie));
allCars.push(new Car('nissan', 'XT', 2012, sam));
allCars.push(new Car('eagle', 'GT', 2011, owen));
allCars.push(new Car('eagle', '9', 2014, finn));
What you can then do is iterate through all of the cars in that array and create a new array with name of that make if it doesnt already exsists and add it to the dictionary like so:
var carsByMake = {};
for(var i=0; i<allCars.length; i++){
if(carsByMake[allCars[i].make] == null){ // if there is no array with the current car make in the dictionary
var newMake = []; // create a new array
newMake.push(allCars[i]); // add current car to it
carsByMake[allCars[i].make] = newMake; // add the array to the dictionary with the key of the current car make
}
else{
carsByMake[allCars[i].make].push(allCars[i]); // else just add to dictionary with the key of current car make
}
}
After that you can iterate through the dictionary and get all the object stored in it by it's key, so for example carsByMake["nissan"] will have stored an array which contains all the cars with the make nissan and so on..
You iterate the dictionary in the following way:
for(var key in carsByMake){ // iterate through dictionary
for(var i=0; i<carsByMake[key].length; i++){ // get all the elements in the current list in dictionary
document.write("carsByMake[" + key + "][" + i + "] = " + carsByMake[key][i].printCar() + "<br>");
}
}
This will print out:
carsByMake[eagle][0] = (Make: eagle, Model: Talon TSi, Year: 1993, Owner: rand)
carsByMake[eagle][1] = (Make: eagle, Model: GT, Year: 2011, Owner: owen)
carsByMake[eagle][2] = (Make: eagle, Model: 9, Year: 2014, Owner: finn)
carsByMake[nissan][0] = (Make: nissan, Model: 300ZX, Year: 1992, Owner: ken)
carsByMake[nissan][1] = (Make: nissan, Model: 54353, Year: 2001, Owner: barbie)
carsByMake[nissan][2] = (Make: nissan, Model: XT, Year: 2012, Owner: sam)
As you can see all the cars of the eagle make are stored in the dictionary by the key of "eagle", and all the cars of the nissan make are stored in the dictionary by the key of "nissan".