从Ionic2存储中获取多个键值

卡普空

使用Ionic2存储,我在自定义提供程序中完成了以下操作,可以确认已设置键值对,并且可以分别检索它们:

this.storage.set('one', 1);
this.storage.set('two', 2);
this.storage.set('three', 3);

问题是我的一个页面中有一个函数需要访问HTTP请求的所有这三个值,因此我可以像这样获得其中之一:

this.storage.get('one').then((value) => {
    // Preform http request sending value in POST data
});

但是,如何一次获得多个键值?我是否需要嵌套这些调用,还是有一种更简单的方法来在一个调用中访问多个键,以便可以执行以下操作:

this.storage.get(['one', 'two', 'three']).then((values) => {
    console.log(values.one); // I dunno, something like this
});
萨姆帕斯

您可以如下所示简单地进行操作。

this.storage.forEach( (value, key, index) => {
    console.log("value", value);//store these values as you wish
    console.log("key", key);
    console.log("Index" index);
})

从Api Doc。

 /**
   * Iterate through each key,value pair.
   * @param iteratorCallback a callback of the form (value, key, iterationNumber)
   * @return Promise that resolves when the iteration has finished.
   */

  forEach(iteratorCallback: (value: any, key: string, iterationNumber: Number) => any): Promise<null> {
    return this._dbPromise.then(db => db.iterate(iteratorCallback));
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章