I'm trying to create a series of objects from a JSON object. My current code looks (something) like the below.
class Card {
constructor(mig, sku, title, price, img, link, promo) {
this.mig = mig;
this.sku = sku;
this.title = title;
this.price = price;
this.img = img;
this.link = link;
this.promo = promo;
}
populateHTML {
return `
<div class="card">
<div class="card--title">
<h2>${this.title}</h2>
</div>
<img src="${this.img}" alt="${this.title}" class="card--img" />
<a href="${this.link}" target="_blank" class="card--link"><button>Click</button></a>
</div>
`;
}
}
const item1 = new Card('MIG5', 'A577', 'item1', '£2.90','/img/S/GRP/IC/AIG5278523.jpg', '/en/test/item1');
const item2 = new Card('MIG3', 'A32', 'item2', '£23.00', '/img/S/GRP/IC/AIG3622835.jpg', '/en/test/item2');
My issue is that when I instantiate a new instance of the class, I have to write out all those parameters within the variable and actually it would be a lot easier to store all of that data within a JSON file or something like that, i.e
{
{
'mig' : 'MIG5',
'sku' : 'A577',
'title' : 'item1',
'price' : '£2.90',
'img' : '/img/S/GRP/IC/AIG5278523.jpg',
'link' : '/en/test/item1'
},
{
'mig' : 'MIG3',
'sku' : 'A382',
'title' : 'item2',
'price' : '£23.00',
'img' : '/img/S/GRP/IC/AIG3622835.jpg',
'link' : '/en/test/item2'}
}
}
Is there a way that something like this can be possible rather than having to manually write out the parameters?
I appreciate all the help you're willing to give!
Thanks