2013-12-17 74 views
0

我想弄清楚如何將多個值推入數組對象。將數據推入數組 - 錯誤:無法調用未定義的方法「推」

var fileCount = 0; 
var limboData = []; //trying to store values in array to create a table 

function XgetAllSites(){ 
    $().SPServices({ 
    operation: "GetAllSubWebCollection", 
    async: false, 
     completefunc: function(xData, Status){ 
     site = $(xData.responseXML); 
     site.find('Web').each(function(){ 
     //var siteName = $(this).attr('Title'); 
     var siteUrl = $(this).attr('Url'); 
     Xgetlists(siteUrl); 
     console.log("At All Sites Level"); //check point 
     }); 
    } 
    }); 
} 
function Xgetlists(siteUrl){ 
    $().SPServices({ 
     operation: "GetListCollection", 
     webURL: siteUrl, 
     async: false, 
     completefunc: function(xData, Status){ 
      $(xData.responseXML).find("List[ServerTemplate='101']").each(function(){ 
      var listId = $(this).attr('ID'); 
      XgetListItems(listId, siteUrl) 
      console.log("At site list"); //check point 
      }); 
     } 
    }); 
} 
function XgetListItems(listId, siteUrl){ 
    $().SPServices({ 
    operation: "GetListItems", 
    webURL: siteUrl, 
    listName: listId, 
    CAMLViewFields: "<ViewFields Properties='True' />", 
    CAMLQuery: '<Query><Where><And><Eq><FieldRef Name="_UIVersionString" /><Value Type="Text">1.0</Value></Eq><IsNotNull><FieldRef Name="CheckoutUser" /></IsNotNull></And></Where></Query>', 
    async: false, 
    completefunc: function (xData,Status){ 
     $(xData.responseXML).SPFilterNode("z:row").each(function() { 
      var fileName = $(this).attr('ows_LinkFilename'); 
      var fileUrl = $(this).attr('ows_FileDirRef'); 
      var checkedTo = $(this).attr('ows_LinkCheckedOutTile'); 
      var modified = $(this).attr('ows_Modifiedff'); 

      limboData[fileCount].push({fileName: fileName, fileUrl:fileUrl,checkedTo:checkedTo,modified:modified}); //trying to store information from returned values in array using fileCount as the index 
      fileCount++; 
      console.log("At list items. File Count: "+fileCount); 
     }); 
    } 
    }); 
} 

回答

1

的問題是,該指數limboData[fileCount]在數組的值是undefined,直到它被設置,所以你不能使用:

limboData[fileCount].push(...); 

相反,你可以設定值由使用索引:

limboData[fileCount] = { 
    fileName: fileName, 
    fileUrl:fileUrl, 
    checkedTo:checkedTo, 
    modified:modified 
}; 

或者通過簡單地調用push

limboData.push({ 
    fileName: fileName, 
    fileUrl:fileUrl, 
    checkedTo:checkedTo, 
    modified:modified 
}); 
+0

工作完美,謝謝。 – Batman

相關問題