LocalForage - 在数组中推送多个项目

时间:2017-05-04 22:34:33

标签: javascript typescript ionic2 localforage

我想在数组中推送多个项目,否则项目会被覆盖。

所以我认为我能做的就是:

   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"));
    })

但是这将替换最后一项,如何在该dataArray中推送多个localForage项?

1 个答案:

答案 0 :(得分:0)

我刚用localForage对它进行了测试,结果正常:

假设var dataArray = [有一些数据]

1)如何使用localForage

中的内容替换现有的dataArray

-(CGPoint)mapSize:(CGSize)assetTrack_size withSize:(CGSize)videoLayer_size forPoint:(CGPoint)overlay_point{ NSLog(@"%f",playerLayer.videoRect.size.width); NSLog(@"%f",playerLayer.videoRect.size.height); NSLog(@"%f",playerLayer.videoRect.origin.x); NSLog(@"%f",playerLayer.videoRect.origin.y); CGFloat assetTrack_width = assetTrack_size.width; CGFloat assetTrack_height = assetTrack_size.height; CGFloat videoLayer_width = videoLayer_size.width; CGFloat videoLayer_height = videoLayer_size.height; CGFloat ratio_width = assetTrack_width/videoLayer_width; CGFloat ratio_height = assetTrack_height/videoLayer_height; CGFloat new_width = overlay_point.x * ratio_width; CGFloat new_height = overlay_point.y * ratio_height; finalOverlayRect = CGRectMake(0, 0,overlayView.frame.size.width * ratio_width , overlayView.frame.size.height * ratio_height) ; CGPoint overlayPoint = CGPointMake(new_width, new_height); return overlayPoint; }

2)如何添加到现有dataArray来自localForage的数据

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

3)如何添加到localForage中的内容

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

希望它有所帮助。