简体   繁体   English

闭环-我放在哪里?

[英]Loop closure - Where do I put it?

In relation to a previous post , I am having a hard time figuring out where to put a loop closure for an array I am using to match against JSON returns. 关于上一篇文章 ,我很难弄清楚在何处放置循环闭包以用于与JSON返回值匹配的数组。

Below is my revised code: 以下是我的修改代码:

$(function()
{
    var $reports = $('#repOut');
    var techCount = 0;
    var repCount = 0;
    techs = ["Name_1", "Name_2"];

    function counts(tech, count)
    {
        this.tech = tech;
        this.count = count;
    }
    $.ajax(
    {
        type: 'GET',
        url: 'https://api.trello.com/1/board/BOARD_ID/checklists?checkItem_fields=name,state&key=MY_KEY&token=MY_TOKEN',
        dataType: 'jsonp',
        success: function(data)
        {
            $.each(data, function(i, repName)
            {
                var items = repName.checkItems;
                for (i = 0; i < items.length; i++)
                {
                    for (var n = 0; n < techs.length; n++)
                    {
                        techName = techs[n];
                    }
                    var rex = new RegExp(techName, "i");
                    var num = /\(\d+\)/;
                    if (rex.test(items[i].name))
                    {
                        var repFull = items[i].name;
                        repName = repFull.replace(/\.*$|-.*$/, "");
                        if (num.test(items[i].name))
                        {
                            var repNum = parseInt(/\d+/.exec(items[i].name), 10);
                            repCount += repNum;
                        }
                    }
                }
            });
            var techCount = new counts(techName, repCount);
            $reports.after("<table border =1 id='reports'><tr><th>Tech</th><th>Count</th></tr><tr><td>" + techCount.tech + "</td><td>" + techCount.count + "</td></td></tr></table>");
        }
    });
});

The loop only returns the JSON values for Name_2 (but this time, it is added up correctly and displayed on my webpage). 该循环仅返回Name_2JSON值(但这一次,它已正确添加并显示在我的网页上)。 I need to get these values for EACH name in my techs array and output them. 我需要在我的techs数组中获取每个名称的这些值并输出。 I have read many websites/articles/answers about closures and I just cannot wrap my head around where/how to put it in my code! 我已经阅读了许多有关闭包的网站/文章/答案,但我只是无法理解将其放置在代码中的位置/方式!

Can anyone help provide an example of what my code would look like with a closure? 任何人都可以提供一个示例,说明我的闭包代码会是什么样子? Am I even right in suspecting a closure is needed? 我怀疑是否需要关闭是否正确?

Thanks for all the feedback! 感谢您的所有反馈! I guess I just needed someone "looking over my shoulder" to help me see the obvious problems. 我想我只需要一个“看着我的肩膀”来帮助我看到明显的问题。 Below is the fixed code with my comments. 下面是带有我的评论的固定代码。

$(function()
{
    var $reports = $('#repOut');
    var techCount = 0;
    var techs = ["Name_1", "Name_2"];

    function counts(tech, count)
    {
        this.tech = tech;
        this.count = count;
    }
    $.ajax(
    {
        type: 'GET',
        url: 'https://api.trello.com/1/board/BOARD_ID/checklists?checkItem_fields=name,state&key=KEY&token=TOKEN',
        dataType: 'jsonp',
        success: function(data)
        {
            for (var n = 0; n < techs.length; n++) //MOVED THE 'tech' LOOP UP HERE
            {
                var techName = techs[n]; //INCLUDING THE 'techName' VARIABLE DEFINITION
                var repCount = 0;
                $.each(data, function(_, repName)
                {
                    var items = repName.checkItems;
                    for (var i = 0; i < items.length; i++)
                    {
                        var rex = new RegExp(techName, "i");
                        var num = /\(\d+\)/;
                        if (rex.test(items[i].name))
                        {
                            var repFull = items[i].name;
                            var repFullName = repFull.replace(/\.*$|-.*$/, "");
                            if (num.test(items[i].name))
                            {
                                var repNum = parseInt(/\d+/.exec(items[i].name), 10);
                                repCount += repNum;
                            }
                        }
                    }
                })
                //MOVED MY OBJECT DECLARATION UP IN THE LOOP
                var techCount = new counts(techName, repCount); 
                $reports.after("<table border =1 id='reports'><tr><th>Tech</th><th>Count</th><tr><td>" + techCount.tech + "</td><td>" + techCount.count + "</td><br></td></tr></table>");
            }
        }
    });
});

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

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