'How to store Map object in local Storage in Electron?

In an electron app, I need to save Map object in local storage. I tried this:

// main.js      
app.on('ready', createWindow);

const createWindow = ) => {
     mainBrowserWindow = new BrowserWindow({
     webPreferences: {
        nodeIntegration: true
        }
     });

    const testMap = new Map([
        ['word1', 1],
        ['word2', 2],
        ['word3', 3],
    ]);
    const jsonTestMap = JSON.stringify(Array.from(testMap.entries()));
    
    mainBrowserWindow.webContents
        .executeJavaScript(`localStorage.setItem("testMap",${jsonTestMap});`)
        .then(() => {
            console.log('OK localStorage.setItem()');
            mainBrowserWindow.webContents
                .executeJavaScript('localStorage.getItem("testMap");')
                .then((val) => {
                    console.log(val);
                    // const res = new Map(JSON.parse(val));
                });
        });
};

but this store testmap as

'word1,1,word2,2,word3,3'

instead of

'[["word1",1],["word2",2],["word3",3]]'

which makes it not possible to make a JSON.parse with localStorage.getItem("testMap")

Could someone please help me and let me know what is the mistake I am making in the above.



Solution 1:[1]

Find it with :

mainBrowserWindow.webContents
    .executeJavaScript(`localStorage.setItem("testMap",'${jsonTestMap}');`)
    .then(() => { ... }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LeMoussel