简体   繁体   English

Node.js循环和JSON构建

[英]Node.js loops and JSON building

Respected ppl .... 尊敬的人....

This is my node.js code ... https://gist.github.com/SkyKOG/99d47dbe5a2cec97426b 这是我的node.js代码... https://gist.github.com/SkyKOG/99d47dbe5a2cec97426b

Im trying to parse the data of our exam results ...example ... http://www.vtualerts.com/results/get_res.php?usn=1MV09IS002&sem=7 我正在尝试解析我们考试结果的数据...示例... http://www.vtualerts.com/results/get_res.php?usn=1MV09IS002&sem=7

Im getting the results ...and i am traversing back for previous seems too ... All works but traversing back happens in random ... prolly something wrong with the loops ... 我正在得到结果...而且我也遍历了以前的内容...除了遍历之外的所有工作都是随机发生的...循环中可能出了点问题...

            json.results = [];

            var output = '';

            var k = response.query.results.body.div.div[0].table[1].tr.length;

            for (var j = 1; j < k; j++) {

                for (var i = 0; i <= 5; i++) {
                    var result_obj = {};
                    result_obj.subjects = [];

                    for (key in response.query.results.body.div.div[0].table[1].tr[j].td[i]) {
                        if (typeof response.query.results.body.div.div[0].table[1].tr[j].td[i].em === "undefined") {
                            continue;
                        }

                        var subject_obj = {};

                        output += "Subject : " + response.query.results.body.div.div[0].table[1].tr[j].td[i].em + " " + "\n";

                        var subtext = response.query.results.body.div.div[0].table[1].tr[j].td[i].em + " " + "\n";
                        subject_obj.subjectname = subtext.replace(/[(].*[)]/, "").trim();

                        result_obj.subjects.push(subject_obj);
                        console.log(subject_obj);
                        break;

                    }

                    console.log(result_obj.subjects);

I presume there is something like async concepts which need to implemented correctly to make the reordering of sems in correct order ... 我认为有一些类似异步的概念,需要正确地实现以正确的顺序对sems重新排序...

And to get the JSON in this format ... https://gist.github.com/SkyKOG/3845d6a94cea3b744296 I dont think im pushing the created objects at the right scope ... 并以这种格式获取JSON ... https://gist.github.com/SkyKOG/3845d6a94cea3b744296我认为即时通讯不会在正确的范围内推送创建的对象...

Kindly help in this regard .... thank you ... 在这方面请帮忙....谢谢...

(I'll answer the ordering part. Suggest making the JSON issue a separate question to fit in with the Q&A format.) (我将回答订购部分。建议使JSON发出一个单独的问题,以适合Q&A格式。)

When you make the HTTP request in your code (see the line below) you're bringing a varying delay into the order that responses are executed 当您在代码中发出HTTP请求(请参见下面的行)时,正在给响应执行的顺序带来不同的延迟

new YQL.exec(queryname, function (response) {

You need to track the order of the requests yourself, or use a library to do it for you. 您需要自己跟踪请求的顺序,或者使用库来为您完成请求。

Code it yourself 自己编写代码

In order to get around that you need something that keeps track of the original order of the requests. 为了解决这个问题,您需要一些可以跟踪请求原始顺序的东西。 Because of the way closures work you can't just increment a simple counter because it'll be changed in the global scope as your loop progresses. 由于闭包的工作方式,您不能只增加一个简单的计数器,因为它会随着循环的进行而在全局范围内更改。 The idiomatic way to solve this is by passing the counter into an immediately executed function (as a value type) 解决此问题的惯用方法是将计数器传递给立即执行的函数(作为值类型)

eg 例如

var responseData = [];
for ( var i = 0; i < 100; i++ ){
    (function(){
        ...
        // http call goes in here somewhere
            responseData[i] = data_from_this_response
        ...
    })(i)
}

Use a library 使用图书馆

Check out the async.parallel() call in Caolan's excellent library. 在Caolan的出色库中查看async.parallel()调用。 You pass it an array of functions and it'll return to your callback with an array of the results. 您将其传递给函数数组,它将返回带有结果数组的回调。

https://github.com/caolan/async/#parallel https://github.com/caolan/async/#parallel

You'll need to create a loop that populates the array with curried versions of your function containing the appropriated variables. 你需要创建一个循环,填充与数组咖喱含有拨付变量的函数的版本。

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

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