0

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?

10
  • 4
    You really should use an array instead. Commented Mar 19, 2019 at 17:30
  • Possible duplicate of How to use a variable for a key in a JavaScript object literal? Commented Mar 19, 2019 at 17:30
  • May I ask why you want to achieve this? You’d get to the same result using arrays, your properties looks like they bear the same kind of objects, after all.... Commented Mar 19, 2019 at 17:42
  • This is a bad idea. Use an array. Commented Mar 19, 2019 at 17:42
  • 1
    @Damodog I mean const slotObj = [[{templateNo: '1', assetId: 'asset-123'}, {templateNo: '4', assetId: 'asset-234'}], […], …]; Commented Mar 20, 2019 at 10:44

4 Answers 4

4

You can use square bracket notation to generate the key:

    slotObj[slotId] = {
        ['slotContent' + i]: { // <-- made unique using iterator ;)
            'templateNo': assetItem.dataset.template,
            'assetId': assetItem.dataset.contentId
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2
let slotObj = {};
let slotId = 'slot1' // hardcoded for SO but this is populated via a parent for loop so would be slot2, slot3 etc
slotObj[slotId] = {};

for (let i = 0; i < assetList.length; i++) {

    let assetItem = assetList[i];

    slotObj[slotId]['slotContent'+i] = 
    { // <-- how to make this unique using iterator ie. slotContent0, slotContent1 etc...
        'templateNo': assetItem.dataset.template,
        'assetId': assetItem.dataset.contentId
    }
}

1 Comment

Thanks @alexey-zelenin this was what I was looking for! :-) The above works perfectly!
1

I don't think, you can achieve this with the JSON syntax. But, you can assign the properties one by one:

for (var i = 0; i < assetList.length; i++) {
  var assetItem = assetList[i];

  slotObj[slotId] = {};
  slotObj[slotId]['slotContent' + i] = {
    'templateNo': assetItem.dataset.template,
    'assetId': assetItem.dataset.contentId
  };
}

4 Comments

Also known as "computed property names" in ES6: es6-features.org/#ComputedPropertyNames
slotObj[slotId] = {}; would definitely need to be outside of the loop
@noviewpoint No, this has nothing to do with computed property names, those are for object literals. Maybe you meant to post your comment on Mattias' answer?
Yep, you're right, I commented on the wrong answer :)
1

You could use a template literal;

  slotObj[slotId] = {
      `slotContent${i}`: { // <-- how to make this unique using iterator ie. slotContent0, slotContent1 etc...
          'templateNo': assetItem.dataset.template,
          'assetId': assetItem.dataset.contentId
      }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.