Home > Blockchain >  How to store value in localStorage with certain format
How to store value in localStorage with certain format

Time:12-20

how can I have a localStorage value stored as

enter image description here

I have the value as: ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew but its like it has an arrow to expand and shows the same value only without the {}, an also how do I insert those keys with the slash and 2dots, have the value inside "", I thought it was an object but after trying to insert the value in a variable

var lsvalue = {
  /: "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew",
}

localStorage.setItem('rmStore', JSON.stringify(lsvalue))

but it did not work, any thoughts?

tried to code mentioned

CodePudding user response:

Store it as a string, the debug tool is automatically interpreting this as an object.

var lsvalue = '{"/":"ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew"}';
localStorage.setItem( 'rmStore', lsvalue )

CodePudding user response:

If I'm understanding this correctly, you want your value to be just this string: "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew".

So, you can just assign that string alone to lsvalue, and set it to localStorage:

var lsvalue = "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew";
localStorage.setItem('rmStore', lsvalue);

Or just set that string as the value:

localStorage.setItem('rmStore', 'ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew');

Both of the above solutions result in the below: enter image description here

But, if you're looking to set an object to localStorage:

    var lsvalue = 
{
    'storedObject' : "ald:20221219_1833|atrv:lMh2Xiq9xN0-is9qy6DHBSpBL3ylttQQew"
}

localStorage.setItem('rmStore', JSON.stringify(lsvalue));

This setup results in the below: enter image description here

  • Related