How do I update local storage array with new items
Hi
Please can you provide a tutorial on how to
1.create an array in local storage
retrieve the contents of the array
update and append to the array
Thanks
Catch up wih the Appery.io community on our forum. Here you'll find information on the lastest questions and issues Appery.io developers are discussing.
https://forum.appery.io/
Hi
Please can you provide a tutorial on how to
1.create an array in local storage
retrieve the contents of the array
update and append to the array
Thanks
Hello Deon,
Please use a method "update" for that: https://docs.appery.io/reference#jque...
1) Read a Storage value to the local variable
2) Add new items to that variable
3) Update the Storage variable with that local variable, using a method "update"
Thanks, but I asked how to Update an Array.
That is not the right link. It is just normal updates of storage variables which I am familiar with.
Hi Deon,
There are many ways to do this. I have been learning more about how to use lodash, its bundled with the app. Another great resource is Stackoverflow
Here is some information for you
SET
localStorage.setItem("localVarName", JSON.stringify(varName));
GET
var varName = JSON.parse( localStorage.getItem("localVarName"));
Put anything into an array using Array.push().
var a=[], b={};
a.push(b);
// a[0] === b;
Extra information on Arrays
Add more than one item at a time
var x = ['a'];
x.push('b', 'c');
// x = ['a', 'b', 'c']
Add items to the beginning of an array
var x = ['c', 'd'];
x.unshift('a', 'b');
// x = ['a', 'b', 'c', 'd']
Add the contents of one array to another
var x = ['a', 'b', 'c'];
var y = ['d', 'e', 'f'];
x.push.apply(x, y);
// x = ['a', 'b', 'c', 'd', 'e', 'f']
// y = ['d', 'e', 'f'] (remains unchanged)
Create a new array from the contents of two arrays
var x = ['a', 'b', 'c'];
var y = ['d', 'e', 'f'];
var z = x.concat(y);
// x = ['a', 'b', 'c'] (remains unchanged)
// y = ['d', 'e', 'f'] (remains unchanged)
// z = ['a', 'b', 'c', 'd', 'e', 'f']
http://stackoverflow.com/questions/62...
Or, something like this would work, I have been implementing MobiScroll which requires working the JS level so I am learning... this is how I working with the array of objects, modify to your needs
function AddItems(data) {
Code: Select all
var list = [];
for (var i = 0; i < data.length; i++) {
list.push({
text1: data[i].text1,
text2: data[i].text2
});
}
}
Jeffry,
Brilliant. This is exactly what I needed. Thank you so much. You have saved me a lot of time with a thorough explanation and sample. I appreciate your assistance.
Here is a sample I did to test your code. The difference is that I am using a local storage array. I am adding and retrieving items which works perfectly. What helped me was your explanation of "x.push" to a variable first before updating the local storage, as there is no way to append items directly to local storage unless you are mapping???
var id = Apperyio.storage.User.get("$['tempUserID']");
x.push(id); //x is already declared as a global variable
Apperyio.storage.userIDlist.update("$['userID']", JSON.stringify(x));
var sendID=Apperyio.storage.userIDlist.get("$['userID']");
var i, s, myStringArray = JSON.parse(sendID), len = myStringArray.length;
for (i=0; i<len; ++i) {
if (i in myStringArray) {
s = myStringArray;
alert(s);
}
}