1

我在 localStorage 中有一个对象数组,我有get一个来自$http get调用的数组。我使用此代码替换localStorage数组中来自$http get

 var arr = res;
     minorObject.forEach(function (arr, i) {
     console.log('above if', arr);
     if (minorObject[i].id === arr.id) {
       arr[i] = minorObject[i];
       console.log('arr after:', arr);
      }
     });

这都在 a.then()var arr = res;始终保持与最初设置的值相同。我不明白arr变量是如何没有更新的。具有正确的console.log('arr after:', arr);值,但是一旦退出循环,就会arr恢复。这是功能

function loadList() {
  $localForage.getItem('minorArray').then(function (res) {
    if (res === null) {
      minorResource.getAll().then(function (res) {
        console.log('minorlist 35', res.minorWorks);
        var minorSync = res.nextSyncDateTime;
        $localForage.setItem('minorArray', res.minorWorks).then(function (arrRes) {
          console.log('arrRes', arrRes);
          minorList.minorArray = arrRes;
          // set nextSync
          $localForage.iterate(function (value, key) {
            if (key === 'nextSync') {
              return value;
            }
          }).then(function (res) {
            // update minor works sync date
            res.mwSync = minorSync;
            $localForage.setItem('nextSync', res);
          });
        })
      });
    } else {
      minorResource.getAll().then(function (res) {
        var minorObject = res.minorWorks,
          minorSync = res.nextSyncDateTime;
        if (minorObject !== null) {
          $localForage.iterate(function (value, key) {
            if (key === 'minorArray') {
              return value;
            }
          }).then(function (res) {
            console.log('response from then promise:', res);
            var arr = res;
            minorObject.forEach(function (arr, i) {
              console.log('above if', arr);
              if (minorObject[i].id === arr.id) {
                arr[i] = minorObject[i];
                console.log('match');
                console.log('arr after:', arr);
              }
            });
            // return $localForage.setItem('minorArray', arr).then(function (res) {
            //   minorList.minorArray = res;
            // });
          });  
4

0 回答 0