I have an object which I need to populate with unique property names and data and to build this dynamically inside a for loop. I'd like to make use of the for loop iterator number to append onto the property name (just to make it unique) but as I'm already adding a variable to the parent property I'm confused on the syntax to achieve this.
This outlines the resulting object structure I'd like to achieve. The properties that need the iterator number adding to are slotContent0, slotContent1 etc.
slotObj
slot1
slotContent0
templateNo: '1',
assetId: 'asset-123'
slotContent1
templateNo: '4',
assetId: 'asset-234'
slot2
slotContent0
templateNo: '1',
assetId: 'asset-456'
slotContent1
templateNo: '4',
assetId: 'asset-678'
slotContent2
templateNo: '4',
assetId: 'asset-879'
slot3
etc...
This is my code so far.
var slotObj = {};
var slotId = 'slot1' // hardcoded for SO but this is populated via a parent for loop so would be dynamic ie. slot1, slot2, slot3 etc
for (var i = 0; i < assetList.length; i++) {
var assetItem = assetList[i];
slotObj[slotId] = {
'slotContent': { // <-- how to make this unique using iterator ie. slotContent0, slotContent1 etc...
'templateNo': assetItem.dataset.template,
'assetId': assetItem.dataset.contentId
}
}
}
So to summarise how do I append the for loop iterator [i]
to my slotContent property?