如果我按如下方式列出对象:
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
var car1 = new Car('eagle', 'Talon TSi', 1993, 'rand');
var car2 = new Car('nissan', '300ZX', 1992, 'ken');
var car3 = new Car('nissan', '54353', 2001, 'barbie');
var car4 = new Car('nissan', 'XT', 2012, 'sam');
var car5 = new Car('eagle', 'GT', 2011, 'owen');
var car6 = new Car('eagle', '9', 2014, 'finn');
如何将具有相同品牌的所有对象推送到我命名的数组,例如:
var nissan = [];
var eagle = [];
甚至可以更好地在make之后命名数组,而不必声明那行代码。
答案 0 :(得分:3)
使用数组而不是car1
,car2
,car3
等。这样您就可以在结果上调用Array#filter
并获取具有特定内容的汽车列表只做:
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
var cars = [
new Car('eagle', 'Talon TSi', 1993, 'rand'),
new Car('nissan', '300ZX', 1992, 'ken'),
new Car('nissan', '54353', 2001, 'barbie'),
new Car('nissan', 'XT', 2012, 'sam'),
new Car('eagle', 'GT', 2011, 'owen'),
new Car('eagle', '9', 2014, 'finn')
]
function isMake (car) {
return car.make === String(this)
}
var nissans = cars.filter(isMake, 'nissan')
var eagles = cars.filter(isMake, 'eagle')
console.log(nissans)
console.log(eagles)

.as-console-wrapper { min-height: 100vh; }

修改:在bejado中指出his answer,您可能打算为您的车主使用报价。我以为他们是代表人的对象。
答案 1 :(得分:0)
一些事情。
首先,我假设所有者是字符串,因此需要引号:
var car1 = new Car('eagle', 'Talon TSi', 1993, 'rand');
其次,不要制作像car4
这样的变量。使用数组。
var cars = [
new Car('eagle', 'Talon TSi', 1993, 'rand'),
...
];
第三,您可以使用filter
方法通过make:
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
var cars = [
new Car('eagle', 'Talon TSi', 1993, 'rand'),
new Car('nissan', '300ZX', 1992, 'ken'),
new Car('nissan', '54353', 2001, 'barbie'),
new Car('nissan', 'XT', 2012, 'sam'),
new Car('eagle', 'GT', 2011, 'owen'),
new Car('eagle', '9', 2014, 'finn')
];
var nissan = cars.filter((car) => car.make == 'nissan');
console.log(nissan);

你可以获得更多的爱好者并创建一个函数,在给定make作为参数的情况下返回汽车:
function carsByMake(make) {
return cars.filter((car) => car.make == make);
}
var nissan = carsByMake('nissan');
var eagle = carsByMake('eagle');
答案 2 :(得分:0)
这是我的变体:
function Car(make, model, year, owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner = owner;
}
var findByMake = function(list, make) {
return list.filter(function(item) {
return (item.make === make);
});
};
var car1 = new Car('eagle', 'Talon TSi', 1993, 'rand');
var car2 = new Car('nissan', '300ZX', 1992, 'ken');
var car3 = new Car('nissan', '54353', 2001, 'barbie');
var car4 = new Car('nissan', 'XT', 2012, 'sam');
var car5 = new Car('eagle', 'GT', 2011, 'owen');
var car6 = new Car('eagle', '9', 2014, 'finn');
var cars = [].concat(car1, car2, car3, car4, car5, car6);
console.log('Make - nissan: ', findByMake(cars, 'nissan'));
var eagle = findByMake(cars, 'eagle');
console.log('Make - eagle: ', eagle);

答案 3 :(得分:0)
您可以添加一个接受另一个汽车对象作为参数的成员方法,如果汽车对于给定的成员属性具有相同的值,则返回true。
实施例
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>";
}
}
此外,我认为将这些汽车对象添加到数组会更加智能,这样您就可以轻松地遍历所有这些对象,并将它们的值与for循环进行比较。
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));
你可以做的是遍历该数组中的所有汽车并创建一个名为make的新数组,如果它还没有存在并将其添加到字典中,如下所示:
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
}
}
之后,您可以遍历字典并通过它的密钥获取存储在其中的所有对象,因此例如carsByMake [&#34; nissan&#34;]将存储一个包含所有对象的数组汽车与日产等等.. 您可以通过以下方式迭代字典:
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>");
}
}
这将打印出来:
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)
正如你所看到的那样,老鹰制造的所有汽车都是通过&#34; eagle&#34;的钥匙存储在字典中的,并且日产制造的所有汽车都通过关键字存储在字典中。 #34;日产&#34;