Home > Back-end >  OOP Javascript Output in Array
OOP Javascript Output in Array

Time:09-14

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.

CodePudding user response:

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

CodePudding user response:

// 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 ****

CodePudding user response:

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!

  • Related