将数据推入数组 - 错误:无法调用未定义的方法'push'

时间:2013-12-17 04:32:22

标签: javascript jquery xml arrays object

我正在试图弄清楚如何将多个值推送到数组对象中。

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 个答案:

答案 0 :(得分: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
});