【问题标题】:Push data into array - Error: Cannot call method 'push' of undefined将数据推送到数组中 - 错误:无法调用未定义的方法“推送”
【发布时间】:2013-12-17 04:32:22
【问题描述】:

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

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);
      });
    }
  });
}

【问题讨论】:

    标签: javascript jquery xml arrays object


    【解决方案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
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多