如何访问另一个文件中对象数组中的数据

时间:2015-09-23 00:18:08

标签: javascript arrays object

基本上,我正在开发一个HTML页面,我必须打印出存储在另一个.js文件中的文本。问题是,文本存储在一个对象中...在一个数组中......在一个函数中......存储在一个变量中。因此,要进行一项数据,需要进行大量的挖掘工作。

以下是随附的.js文件中的数据(或类似内容):

var TestDataSet= (function() {
  var reviews = [
    { Id: "abcd1234", 
      Title: "This Is Title Text", 
      Number: 5,
      Body: "text",
      CreateDate: new Date(2012,5,23,14,12,10,0),
      Owner: {
        Id: "Person1234", 
        Name: "James Smith", 
      }
    },
  ]

如何让浏览器从First ID返回“abcd1234”?嵌套ID中的“Person1234”怎么样?

到目前为止我得到的最好的是:

var data1 = new reviews;
console.log(data1.reviews[0].Id);

但这没有任何作用。我得到了很多“未定义”的错误。

1 个答案:

答案 0 :(得分:0)

显然你还没有发布你的完整功能。 但是,我建议像在getter函数中一样返回数组值,如果对于脚本的其余部分是安全的,则使用它:

<!DOCTYPE html> 

<%@ Page language="C#" %> 

<%@ Register Tagprefix="SharePoint" 

     Namespace="Microsoft.SharePoint.WebControls" 

     Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 

<%@ Import Namespace="Microsoft.SharePoint" %> 

<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"> 

<head> 

<meta name="WebPartPageExpansion" content="full" /> 

    <!-- the following 5 js files are required to use CSOM --> 

    <script type="text/javascript" src="/_layouts/1033/init.js"></script> 

    <script type="text/javascript" src="/_layouts/MicrosoftAjax.js"></script> 

    <script type="text/javascript" src="/_layouts/sp.core.js"></script> 

    <script type="text/javascript" src="/_layouts/sp.runtime.js"></script> 

    <script type="text/javascript" src="/_layouts/sp.js"></script> 

 <script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script> 

<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest> 



    <script type="text/ecmascript"> 







  window.onload = function OnLoad() { 





SP.SOD.executeFunc('sp.js','SP.ClientContext',retrieveListItems()); 



var oList; 



function retrieveListItems() { 



var Url = '/Policies'; 



    var clientContext = new SP.ClientContext(Url);     //.(Url); //SP.ClientContext.get_current(); 



   var oList = clientContext.get_web().get_lists().getByTitle('HRPolicies'); 
 var oListTags = clientContext.get_web().get_lists().getByTitle('TopicsSF'); 

//var field = oList.get_fields().getByInternalNameOrTitle("HRTopics");    
//var lookupField = clientContext.CastTo<FieldLookup>(field); 

//var childIdField = oList["HRTopics"] as FieldLookupValue[];

//if (childIdField != null)
//{
 //   foreach(var lookupValue in childIdField)
  //  {
  //      var childId_Value = lookupValue.LookupValue;
  //      var childId_Id = lookupValue.LookupId;
  //  }
//}

//end if no working, try field itself as getllokup as

    var camlQuery = new SP.CamlQuery(); 

   camlQuery.set_viewXml(multiLookupValues); 

   // this.collListItem = oList.getItems(camlQuery); 

     var PertinentItems = oList.getItems(camlQuery); 

clientContext.Load(item, i => i["HRTopics"]);
       clientContext.Load(items);
        clientContext.ExecuteQuery();
        var lookupValues = new ArrayList();
        FieldLookupValue[] values = item["HRTopics"] as FieldLookupValue[];
        foreach (ListItem listItem in items)
        {
            var lookupValue = new FieldLookupValue { LookupId = listItem.Id };
            lookupValues.label;
        }
        item.ParseAndSetFieldValue(lookupColumnName, null);
        item["HRTopics"] = lookupValues.ToArray();
        ctx.ExecuteQuery();

 //   var lookupListId = new Guid(lookupField.LookupList);
  //  var lookupList = clientContext.Web.Lists.GetById(lookupListId);
//
  //  clientContext.load(collListItem,lookupField); 

   // works alert("birds"); 



   // clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);        
//




//} 



function onQuerySucceeded(sender, args) { 



   var listItemInfo = ''; 



    var listItemEnumerator = collListItem.getEnumerator(); 



   while (listItemEnumerator.moveNext()) { 

        var oListItem = listItemEnumerator.get_current(); 

        listItemInfo += '\nID: ' + oListItem.get_id() + 

            '\nTitle: ' + oListItem.get_item('Title') + 
 '\nHRTopics: ' + oListItem.get_item('HRTopics') 
    } 





  document.getElementById('realData').innerHTML = listItemInfo.toString();



} 



function onQueryFailed(sender, args) { 



    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 

} 

} 





    </script> 





<!--[if gte mso 9]> 

<SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,WikiField,_dlc_DocId,_dlc_DocIdUrl,_dlc_DocIdPersistId"><xml>







































<mso:CustomDocumentProperties> 

<mso:_dlc_DocId msdt:dt="string">SCP5UENJTFED-1359-20</mso:_dlc_DocId> 

<mso:_dlc_DocIdItemGuid msdt:dt="string">7d65ee24-642b-450f-8285-a1fd946836f4</mso:_dlc_DocIdItemGuid> 

<mso:_dlc_DocIdUrl msdt:dt="string">site/_layouts/DocIdRedir.aspx?ID=SCP5UENJTFED-1359-20, SCP5UENJTFED-1359-20</mso:_dlc_DocIdUrl> 

</mso:CustomDocumentProperties> 

</xml></SharePoint:CTFieldRefs><![endif]--> 

</head> 

<body> 

   <div id="realData">

     </div>   

</body> 

</html>​

});

然后您可以使用以下方式访问数据:

var TestDataSet= (function() {
var reviews = [
    { Id: "abcd1234", 
    Title: "This Is Title Text", 
    Number: 5, Body: "text",
    CreateDate: new Date(2012,5,23,14,12,10,0),
    Owner: {
    Id: "Person1234", 
    Name: "James Smith", 

    }
},];
return reviews;