2017-07-12 174 views
0

在req.files所以我想迭代這req.files樣子,當我 conosle.log(req.files)獲得訪問所有變量循環內循環外

{ logo: 
 
    File { 
 
    domain: 
 
     Domain { 
 
     domain: null, 
 
     _events: [Object], 
 
     members: [Object] }, 
 
    path: '/var/folders/v7/qqwh5nds511b28p8r4006pq40000gn/T/upload_3e7a4c1ee6461ad12c70140857d925cc', 
 
    name: 'logo.png', 
 
     }, 
 
    homeSliderImage: 
 
    File { 
 
    domain: 
 
     Domain { 
 
     domain: null, 
 
     _events: [Object], 
 
     members: [Object] }, 
 
    path: '/var/folders/v7/qqwh5nds511b28p8r4006pq40000gn/T/upload_424f4be7ef9a8cc1331da4ca126cd806', 
 
    name: 'apple.png', 
 
     } 
 
}

我在這裏有我的循環,我通過req.files循環,我得到的鑰匙後,我想獲得每個人的路徑的鑰匙對象(徽標和homeSliderImage)的關鍵。我所做的是,但我希望能夠在這個循環之外訪問這兩個路徑。如果我在循環外部console.log(newPath)它只會記錄最後一個key.path,我想要所有這些。

var file = req.files 
 

 
     for (var key in file) { 
 
      if (file.hasOwnProperty(key)) { 
 
      var newPath = req.files[key].path 
 
     
 
      } 
 
     }

+0

好聽起來像你需要一組值 – epascarello

回答

2

你需要一個集合(如數組),這應該在循環外部進行初始化,因此該值不是每次都重設,如:

var paths = []; // array to hold all the paths 

var file = req.files  

for (var key in file) { 
    if (file.hasOwnProperty(key)) { 
    paths.push(req.files[key].path); // add the value to the paths array 
    } 
} 
+0

感謝這工作:) –