0

How to use the variable presentationtitle as key in my else condition within the object given its a string?

I tried if (typeof data.presentations.'(presentationtitle)'

let presentationtitle = 'Test presentation 123'  

let variable = { 
       presentations: {
          [presentationtitle] : {
              duration: '00:00:00',
          }
       },
    };

chrome.storage.sync.get('presentations', function(data) {
  
if (typeof data.presentations === 'undefined') {
    // if not set then set it 
    let extensionsettings = { 
       presentations: {
          [presentationtitle] : {
          }
       },
        general: {
          setting1: true,
          setting2: false
        },
      };

    chrome.storage.sync.set(extensionsettings, () => {
          }); 
    } 
else {
    chrome.storage.sync.get('presentations', function(data) {

    if (typeof data.presentations.'(presentationtitle)' === 'undefined') {
    // if not set then set it 
        
    let presentationtitlesave = { 
       presentations: {
          [presentationtitle] : {
              duration: '00:00:00',
          }
       },
       };
          
    chrome.storage.sync.set(presentationtitlesave, () => {
          });
    }

    });
}

Basically I first save an object for extensionsettings (for first time use of the extension opening the popup) and thereafter I want to add presentationtitles into it if they are not present yet (which I extract them from DOM HTML).

Markus
  • 458
  • 4
  • 16
  • 3
    Use bracket notation [`data.presentations[presentationtitle]`](https://stackoverflow.com/questions/17189642/difference-between-using-bracket-and-dot-notation) for the `if` part. – Lain Nov 08 '22 at 14:53
  • thanks @Lain sorry forgot to mention I already tried bracket notation which didn't work due to `Unexpected token '['` but now realised with your answer I have to use it without the dot in front of it, cheers that worked! The only issue I now have is the `presentations` objects gets overwritten instead of adding only the addition. – Markus Nov 08 '22 at 15:32

0 Answers0