3

If I make a list of objects as follows:

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

How can I push all objects with the same make to an array that I name, eg:

var nissan = [];
var eagle = [];

Or even better naming the array after the make without having to declare that line of code.

4 Answers 4

3

Use an array instead of car1, car2, car3, etc. This allows you to call Array#filter on the result and get a list of cars with a certain make only:

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

Edit: As bejado points out in his answer, you probably meant to use quotes for the owners of your car. I had assumed they were objects representing people.

Sign up to request clarification or add additional context in comments.

Comments

0

Here's my variation:

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

Comments

0

A few things.

First, I'm assuming owners are strings and thus need to be in quotes:

var car1 = new Car('eagle', 'Talon TSi', 1993, 'rand');

Second, don't make variables like car4. Use an array.

var cars = [
  new Car('eagle', 'Talon TSi', 1993, 'rand'),
   ...
];

Third, you can use the filter method to filter your car array by 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);

You can get a bit fancier and create a function that returns cars given the make as a parameter:

function carsByMake(make) {
    return cars.filter((car) => car.make == make);
}
var nissan = carsByMake('nissan');
var eagle = carsByMake('eagle');

Comments

0

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

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.