2017-07-17 120 views
-3

我想用數組中的新對象替換一些現有對象,我有2個數組allDaysstoredDay,兩者的大小不同allDays將10個對象和storedDays將有4個對象,但是兩者都將具有相同的價值day在數組中匹配兩個值

mode(){ 
     this.nativeStorage.getItem("modifiedData").then((data)=>{ 
      console.log(data) 
      var allDay = this.dailyDays; 
      for (var i = 0; i < allDay.length; i++) { 
       var element = allDay[i]; 
       var storedDay = data[i]; 
       console.log("all Day",element.day); 
       console.log("stored Day",storedDay.day); 
      if (element.day === storedDay.day) { 
        console.log("we have same day", i) 
        data=({ 
         day: data.day, 
         month: data.month, 
         year: data.year, 
         quantity: data.selectedQuantity, 
         brand: data.selectedBrand, 
         price: data.price, 
        }) 
        // below line will replace the whole object 
        this.dailyDays[i] = data; 
       } 

      } 
      console.log(data.day) 
     }) 
    } 

上執行我在儲值得到錯誤上面,可能有人給例子,如何檢查兩個數組,並與storedDay更新數組。

此圖片的第一行是存儲日期數組,包含4個對象。 而阿迪陣列將有10個對象 enter image description here

+0

什麼這種價值存儲日期?如果它是一個對象,你不能把它與'===' – Danmoreng

+1

告訴我們錯誤 –

+0

是這些長度相同的數組? –

回答

3

您必須定義新的變量,用於存儲當天值:

mode(){ 
    this.nativeStorage.getItem("modifiedData").then((data)=>{ 
     console.log(data); 
     var sameDay = null; // =======> define this variable 
     var allDay = this.dailyDays; 
     for (var i = 0; i < allDay.length; i++) { 
      var element = allDay[i]; 
      var storedDay = data[i]; 
      console.log("all Day",element.day); 
      console.log("stored Day",storedDay.day); 
     if (element.day === storedDay.day) { 
       console.log("we have same day", i) 
       sameDay=({ // ==========> change this 
        day: data.day, 
        month: data.month, 
        year: data.year, 
        quantity: data.selectedQuantity, 
        brand: data.selectedBrand, 
        price: data.price, 
       }) 
       // below line will replace the whole object 
       this.dailyDays[i] = sameDay; // ======> change this 
      } 

     } 
     if(sameDay) 
      console.log(sameDay.day) // =======> and change this 
    }) 
} 
0

我設法找到解決這一設置爲我的要求

mode(){ 
     this.nativeStorage.getItem("modifiedData").then((data)=>{ 
      console.log(data) 
      var sameDay; 
      var allDay = this.dailyDays; 
      for (var i = 0; i < allDay.length; i++) { 
       var allDayelement = allDay[i]; 

       console.log("all Day",allDayelement.day); 

       data.forEach(forEachElement => { 
        console.log("forEachElement",forEachElement.day); 
        if (allDayelement.day === forEachElement.day) { 
        console.log("we have same day", i) 
        sameDay=({ 
         day: forEachElement.day, 
         month: forEachElement.month, 
         year: forEachElement.year, 
         quantity: forEachElement.selectedQuantity, 
         brand: forEachElement.selectedBrand, 
         price: forEachElement.price, 
        }) 
        // below line will replace the whole object 
        this.dailyDays[i] = sameDay; 
      } 
       }); 

      } 
     // console.log(data.day) 
     }) 
    }