Posts Tagged ‘key-value’

Below is one simple function that saves my required json in localstorage using JavaScript array.


function saveMyJson(key,value)
{

// initialise array
var myObj = {};

// get existing json from local storage
if(localStorage.getItem('my_json') != null)
myObj = JSON.parse(localStorage.getItem('my_json'));

// add element in json by key-value pair
eval("myObj['" + key + "'] = \""+ value + "\"; ");

// set updated json in local storage
localStorage.setItem('my_json', JSON.stringify(myObj));

}

 

Advertisements