简体   繁体   English

如何使用javascript / jquery打印数据

[英]How to print data using javascript/jquery

I have the next js code. 我有下一个js代码。

var myData=[];
for(j=1;j<=Pages;j++)
{
$.ajax({
        type: 'get',
        url: 'link.xml'+j,
        async: false, 
        dataType: 'xml', 
        success: function(data){
            getData(data);
        }, 
        error: function(data){
            console.log("error");
        }
    });
}
function getData(data){


     var tmpData = {id:'' , displayName:''};  

     //taking the values and storing them to myData


            myData.push(tmpData); 
            tmpData = {id:'' , displayName:'', eventsHref: []};


}


    for(i=0;i<myData.length;i++)
    {
     $.ajax({
        type: 'get',
        url: 'somelink'+data[i].id,
        async: false, 
        dataType: 'xml', 
        success: function(events){
            getUpEvents(events);
        }, 
        error: function(events){
            console.log("error");
        }
    });
    }
    function getUpEvents(events){

    var tmpEvents = {displayNameEvent:[] , displayNameArtist: []};

     //taking some other values and storing them to myData

     myData.push(tmpEvents); 
     tmpEvents = {displayNameEvent:[] , displayNameArtist:[]};

}

Now to print the results from myData with a specific way.In the first line myData[0].displayName.Next lines all the myData[i].displayNameEvents that indicates to the myData[0].displayName and all the myData[i].displayNameArtist.After that it will print the next myData[1].displayName and so goes on. 现在以特定方式打印myData的结果。在第一行myData [0] .displayName。接下来的行中,所有指示myData [0] .displayName和所有myData [i]的myData [i] .displayNameEvents .displayNameArtist。之后,它将打印下一个myData [1] .displayName,依此类推。

Below is how i tried to print them. 以下是我尝试打印它们的方式。

for(i=0;i<myData.length;i++)
{
        document.write(myData[i].displayName+"<br>");
        document.write(myData[i].displayNameEvent+"<br>");
        document.write(myData[i].displayNameArtist+"<br>");
}

If this is just for debugging, use JSON.stringify(myData) to parse your data into a JSON string. 如果这仅用于调试,请使用JSON.stringify(myData)将您的数据解析为JSON字符串。 This way you'll have both the property name and value right next to eachother. 这样,您将使属性名称和值彼此相邻。

Then use console.log or div.innerText to write the text out. 然后使用console.logdiv.innerText将文本写出。

For example, assuming you have a div with the class 'output': 例如,假设您有一个带有“输出”类的div:

$('.output').text(JSON.stringify(myData));

You can also open your debugger console and view the console output with: 您还可以打开调试器控制台,并使用以下命令查看控制台输出:

console.log(JSON.stringify(myData));
//Create a div to hold the results
var results = $("<div/>");

//Loop through the data and append each value wrapped in a p to the div
for(i=0;i<myData.length;i++)
{
        results.append("<p>" + myData[i].displayName + "</p>");
        results.append("<p>" + myData[i].displayNameEvent + "</p>");
        results.append("<p>" + myData[i].displayNameArtist + "</p>");
}

//append the div to the body
$("body").append(results);

In the myData array above, two elements are being pushed for each object. 在上面的myData数组中,为每个对象推送了两个元素。

  • one object, which has id and displayName properties 一个具有id和displayName属性的对象
  • second object with displayNameEvent and displayNameArtist array 第二个具有displayNameEvent和displayNameArtist数组的对象

As there is no relation between these two array elements, we cannot print the data corrrectly, displayName with its associated diasplayNameEvents and displayNameArtists. 由于这两个数组元素之间没有关系,因此我们无法正确打印数据displayName及其关联的diasplayNameEvents和displayNameArtists。

I have rewritten the above code with some dummy data in JsFiddle . 我已经在JsFiddle中用一些伪数据重写了上面的代码。 The code associates the id / displayName with displayNameEvent / displayNameArtist arrays and prints the data correctly as shown below. 该代码将id / displayName与displayNameEvent / displayNameArtist数组相关联,并正确打印数据,如下所示。

101s name

101s event1, 101s event2

101s artist1, 101s artist2


102s name

102s event1, 102s event2

102s artist1, 102s artist2


103s name

103s event1, 103s event2

103s artist1, 103s artist2

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM