Home > database >  Returning all objects within an array
Returning all objects within an array

Time:02-01

I'm new to JavaScript and working on a personal program which creates car objects and stores them in an array, I am having issues with returning all array elements as only the first array element is returned.

const carFactory = {
    _cars:[
        {
            make: 'default',
            model: 'default',
            year: 0,
        }
    ],

    get cars(){
        if(this._cars.length > 0 ){
            for(let i = 0; i < this._cars.length; i  ){
                return `Car Make: ${this._cars[i].make} - Car Model: ${this._cars[i].model} Manufacture Year: ${this._cars[i].year}`;
                }
        }else{
            return `Please add car details`;
            }
    },

    addCar(carMake, carModel, carYear){
        this._cars.push({
            carMake,
            carModel,
            carYear
        })
    }
}

carFactory.addCar('Toyota', 'Corolla', 2003);

console.log(carFactory.cars);

CodePudding user response:

The issue with your code is that the return statement inside the for loop only returns the first car object in the _cars array and terminates the loop. To return all cars, you can concatenate the car objects into a string and return it after the loop:

const carFactory = {
    _cars:[
        {
            make: 'default',
            model: 'default',
            year: 0,
        }
    ],

    get cars(){
        if(this._cars.length > 0 ){
            let allCars = '';
            for(let i = 0; i < this._cars.length; i  ){
                allCars  = `Car Make: ${this._cars[i].make} - Car Model: ${this._cars[i].model} Manufacture Year: ${this._cars[i].year}\n`;
            }
            return allCars;
        }else{
            return `Please add car details`;
        }
    },

    addCar(carMake, carModel, carYear){
        this._cars.push({
            make: carMake,
            model: carModel,
            year: carYear
        })
    }
}

carFactory.addCar('Toyota', 'Corolla', 2003);

console.log(carFactory.cars);

Output:

Car Make: default - Car Model: default Manufacture Year: 0
Car Make: Toyota - Car Model: Corolla Manufacture Year: 2003

CodePudding user response:

You have a return in your for, which will exit the loop at the first iteration:

for(let i = 0; i < this._cars.length; i  ){
  return `Car Make: ${this._cars[i].make} - Car Model: ${this._cars[i].model} Manufacture Year: ${this._cars[i].year}`;
 }

CodePudding user response:

if you want an array in return then return an array. and addCar method need to have right name.

const carFactory = {
    _cars: [
        {
            make: 'default',
            model: 'default',
            year: 0,
        }
    ],

    get cars() {
        if (this._cars.length > 0) {
            let allCars = [];
            for (let i = 0; i < this._cars.length; i  ) {
                let c = `Car Make: ${this._cars[i].make} - Car Model: ${this._cars[i].model} Manufacture Year: ${this._cars[i].year}`;
                allCars.push(c)
            }
            return allCars
        } else {
            return `Please add car details`;
        }
    },

    addCar(make, model, year) {
        this._cars.push({
            make,
            model,
            year
        })
    }
}

carFactory.addCar('Toyota', 'Corolla', 2003);
console.log(carFactory.cars);

  • Related