I’m trying to save several data array object to sqlite storage. I can manage to save just one but whenever I try to save another one, it overrides previous one with the same key name. I have to make the key value dynamic. How can I do that?
Here’s my data provider ts file.
private options: any[] = [
{
"name": "option 01",
"website": "www.google.com",
"about": "choose this option 01",
"id": "1"
},
{
"name": "option 02",
"website": "www.yahoo.com",
"about": "choose this option 02",
"id": "2"
},
{
"name": "option 03",
"website": "www.bing.com",
"about": "choose this option 03",
"id": "3"
},
{
"name": "option 04",
"website": "www.stackoverflow.com",
"about": "choose this option 04",
"id": "4"
}
]
and here’s my home.ts file. It saves data object well but right now, it can save only one. I want to be able to save several and delete each dynamically using different key value per a data object.
setValue(){
this.storage.set("object",this.option).then((successData)=>{
console.log("Data Stored!");
console.log(successData);
})
}
getValue(){
this.storage.get("object").then((data)=>{
console.log(data);
})
}
removeValue() {
this.storage.remove("object").then((data)=> {
console.log("data removed!");
})
}
Thanks in advance, Ideally, I’m looking into save several data objects into the same key value with different ids… but unfortunately, if a key value is same, it will override the previous data object.
Maybe I am missing more context here but just try to make your funcitons to accept unique key name of option you need (I called it optionNumber).
See below.
setValue(optionNumber){
this.storage.set("option"+optionNumber,this.options[optionNumber]).then((successData)=>{
console.log("Data Stored!");
console.log(successData);
})
}
getValue(optionNumber){
this.storage.get("option"+optionNumber).then((data)=>{
console.log(data);
})
}
removeValue(optionNumber) {
this.storage.remove("option"+optionNumber).then((data)=> {
console.log("data removed!");
})
}
Now once you have your options array (this.options) with data, when you need to store specific option just do:
setValue(0)
Please note since array index starts at 0, you might need to adjust for that if you can't have option 0.