简体   繁体   中英

LocalForage - push multiple items in array

I want to push multiple items in an array otherwise the item gets overrided.

So what I thought I could do is this:

   localForage.getItem("data", (err, results) => {
        console.log('results', results)
        // var dataArray = results ? results : [];
        // data.push(results);
        this.dataArray.push(results);
        localForage.setItem("data", results);
        console.log(localForage.getItem("data"));
    })

But this will replace the last item, how can I push multiple localForage items in that dataArray?

I've just tested this with localForage and it works:

assuming var dataArray = [with some data]

1) how toreplace existing dataArray with what you have from the localForage

localForage.getItem("data").then((results) => { dataArray = [].concat(results); });

2) how to add to your existing dataArray the data from the localForage

localForage.getItem("data").then((results) => { dataArray = dataArray.concat(results); });

3) how to add to what you have in the localForage

localForage.getItem("data").then((result) => {
    dataArray = dataArray.concat(result);
    localForage.removeItem("data");
    localForage.setItem("data", dataArray);
  });

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM