环闭合 - 我在哪里可以把它? [英] Loop closure - Where do I put it?

查看:126
本文介绍了环闭合 - 我在哪里可以把它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在相对于<一href=\"http://stackoverflow.com/questions/30635488/javascript-trello-json-array-loop-counter-scope\">$p$pvious帖子,我有一个很难搞清楚在哪里把一个循环闭合的我用来匹配的 JSON 返回一个数组。

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.

下面是我修改code:

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

循环只返回 JSON NAME_2 (但是这一次,它是正确相加,在我的网页显示)。我需要得到这些值的每个命名在我的技术人员阵列并输出。我读过关于关闭许多网站/用品/答案,我只是不能换我的头周围哪里/如何把它放在我的code!

The loop only returns the JSON values for Name_2 (but this time, it is added up correctly and displayed on my webpage). I need to get these values for EACH name in my techs array and output them. 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!

谁能帮助提供一下我的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?

推荐答案

感谢所有的反馈!我想我只是需要有人看着我的肩膀帮我看到明显的问题。下面是固定的code。与我的意见。

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

这篇关于环闭合 - 我在哪里可以把它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆