0

Can you please show me a simple code which add unlimited values into JavaScript objects. I don't want single view "Toyota, 2014". I want show lists value after I add value from textbox.

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

At code below, this is single value to add, but I need add many value doesn't matter, how many of number I want to add.

const myCar = new Car("Toyota", 2014); 

I expect to view many different values like

Toyota, 2014
Opel, 2018
Volkswagen, 2021
and many mores
  • 1
    Try with [Arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) – D.Schaller Nov 26 '21 at 12:50

1 Answers1

0

It sounds like you want an array of Car objects:

const cars = [];

Then adding a Car is:

cars.push(new Car(make, year));
// Or some people do, which also works:
cars[cars.length] = new Car(make, year);

...where make and year come from the text boxes.

When you want to list them all, you'll use some kind of loop (the exact kinds depends on how you're listing them). My answer here shows the various ways you can loop through arrays.

More about arrays on MDN.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875