adding objects to array in localStorage

后端 未结 5 1811
轮回少年
轮回少年 2021-01-11 10:44

Ok, I\'ve tried to follow examples here, I know there might be a few different ways of adding objects to an array in localstorage and not overwriting it but I\'m failing to

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-11 11:27

    adding objects to array in localStorage (Ionic):

    var existingEntries = JSON.parse(localStorage.getItem("allEntries"));
    if(existingEntries == null) existingEntries = [];
    var testObject ={username:this.username, 
    mobile:this.mobile,
    email:this.email,
    type:this.type,
    password:this.password};
    
    localStorage.setItem('testObject', JSON.stringify(testObject));
    existingEntries.push(testObject);
    localStorage.setItem("allEntries", JSON.stringify(existingEntries));
    

提交回复
热议问题