Home > Software engineering >  how to make an object of fix length in local storage
how to make an object of fix length in local storage

Time:01-06

I want to store my radio button values in an object on local storage with index of the object as key of it. what should I do to make fix size array. also I want to set that object as initial value of my state. I have tried some things, but it does not work out.

CodePudding user response:

var obj = {'one': 1,'two': 2,'three': 3 };

localStorage.setItem('testObject', JSON.stringify(testObject));

var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));

CodePudding user response:

this is like splicing array if there is already a value.

const obj2 = JSON.parse(localStorage.getItem("ID") || "[]")
     obj2[index] ? obj.splice(index,1,obj1) : obj.push(obj1)
    localStorage.setItem('ID', JSON.stringify(obj2))

  • Related