From an array of Car instances how to filter such items by e.g. year and model?

Viewed 71

Hi guys I have pushed a class/object into an array like below:

class AllCar  {
  constructor(){
    this.carList=[]
  }
  addCar(newCar){
    this.carList.push(newCar)
  }
}

class Car {
  constructor(name, year, model) {
    this.name = name;
    this.year = year;
    this.model = model;
  }
}

const myCar = new Car("Ford", 2014, "Mustang");
const myCar2 = new Car("Toyota", 2014, "Vios");
const myCar3 = new Car("Honda", 2014, "City");
console.log(myCar)
console.log(myCar.model)
const myCars= new AllCar()
myCars.addCar(myCar)
myCars.addCar(myCar2)
myCars.addCar(myCar3)
console.log(myCars.carList)

before the class/object went into the array. I am able to output console.log(myCar.model) for the model only. After the push into the array, I am not able to select from the property but I can only show the whole list([Car {...}, Car {...}, Car {...}]). How do I filter the carList (e.g. filter the year "2014") and show the output model only (["Mustang","Vios","City"]) or name only["Ford","Toyota","Honda"] without the key.

4 Answers
// You can use the filter method present in javascript for arrays.

// **** YOUR CODE START ****
class AllCar {
  constructor() {
    this.carList = [];
  }
  addCar(newCar) {
    this.carList.push(newCar);
  }
}

class Car {
  constructor(name, year, model) {
    this.name = name;
    this.year = year;
    this.model = model;
  }
}

const myCar = new Car("Ford", 2014, "Mustang");
const myCar2 = new Car("Toyota", 2013, "Vios");
const myCar3 = new Car("Honda", 2011, "City");
console.log(myCar);
console.log(myCar.model);
const myCars = new AllCar();
myCars.addCar(myCar);
myCars.addCar(myCar2);
myCars.addCar(myCar3);
console.log(myCars);

// ****YOUR CODE END ****

// *** WHAT YOU CAN DO START ****

const filterCar = (UserYear) => {
  let filteredCar = myCars.carList.filter((item) => item.year === UserYear);
  console.log(filteredCar);
};
filterCar(2014);

// *** WHAT YOU CAN DO END ****

Use array methods

class AllCar  {
  constructor(){
    this.carList=[]
  }
  addCar(newCar){
    this.carList.push(newCar)
  }
}

class Car {
  constructor(name, year, model) {
    this.name = name;
    this.year = year;
    this.model = model;
  }
}

const myCar = new Car("Ford", 2014, "Mustang");
const myCar2 = new Car("Toyota", 2014, "Vios");
const myCar3 = new Car("Honda", 2014, "City");
const myCars= new AllCar()
myCars.addCar(myCar)
myCars.addCar(myCar2)
myCars.addCar(myCar3)
console.log(myCars.carList.filter(car => car.year === 2014).map(car => car.model))

Use Array.prototype.map and Array.prototype.filter.

[…]
const myCars= new AllCar()
myCars.addCar(myCar)
myCars.addCar(myCar2)
myCars.addCar(myCar3)

console.log(myCars.carList.filter(car => car.year === 2014)); // lists only cars made in 2014

console.log(myCars.carList.map(car => car.model)); // Show only model names

console.log(myCars.carList.filter(car => car.year === 2014).map(car => car.model)); // you can chain them!

From my above comment ...

"In addition to all the other comments and answers there is no need for an additional AllCar class. Especially how it(s sole instance) gets used within the example code, such a class just does not make any sense. "

A possible way of tracking Car instances could look similar to the next provided code example ...

const Car = (() => {

  // module scope / (local) function scope.
  const carList = [];

  // export.
  return class Car {
    constructor(name = '', year = null, model = '' ) {
      Object.assign(this, { name, year, model });
      carList.push(this);
      return this;
    }
    deregister() {
      const idx = carList
        .findIndex(car => (car === this));

      if (idx >= 0) {
        const dump = carList.splice(idx, 1);

        Reflect.deleteProperty(dump[0], 'name');
        Reflect.deleteProperty(dump[0], 'year');
        Reflect.deleteProperty(dump[0], 'model');
        // Reflect.deleteProperty(dump, '0');

        dump.length = 0;
      }
    }
    static getInstances() {
      // return just a shallow copy
      // of the `Car` instances array.
      return [...carList];
    }
  };
})();

new Car("Ford", 2014, "Mustang");
new Car("Toyota", 2015, "Vios");
new Car("Honda", 2014, "City");

console.log(
  Car
    .getInstances()
    .filter(({ year }) => year === 2014)
    .map(({ model }) => model)
);
const [ myCar, myCar2, myCar3 ] = Car.getInstances();

console.log({
  carList: Car.getInstances(),
  myCar,
  myCar2,
  myCar3,
});

myCar2.deregister();
myCar.deregister();

console.log({
  carList: Car.getInstances(),
});
.as-console-wrapper { min-height: 100%!important; top: 0; }

Related