0

given the code below, is there any way i can push objects into an array using a loop that builds the object name dynamically?

I mean, push objects plant 1 to plant 5 into the array using a loop. Right now, the code just push the string into the array.. and i want to reference the existing object..

I would much appreciate any help.

function Plants( name , price, type) {
   name;
   price;
   type;
}

const plant1 = new Plants ("Monstera Deliciosa", 4500, "interior");
const plant2 = new Plants ("Monstera Adansonii", 1800, "interior");
const plant3 = new Plants ("Sanseviera", 800, "interior");
const plant4 = new Plants ("Pilea", 2000, "interior");
const plant5 = new Plants ("Euphorbia", 2500, "interior");

const plantsCatalog = []

    for (i = 1; i<=5; i++){
        let objplant = `plant${i}`
    plantsCatalog.push(objplant)
    
    console.log(objplant)
  }


pitziro
  • 1
  • 1

0 Answers0