0

我完全不知道怎么问这个问题。如果有人不明白这一点,我很抱歉。事情是,我有对象,我正在循环使用each function,当我得到子对象(内部对象)时,我想分配回相同each function的对象,有什么好主意?

看看我的问题:

我的对象:

var xploreMaps = {
    radious:55,
    stroke:5,strokeColor:'#fff',
    opacity:0.8,fontSize:13,line:10,
    cGtext:{
        length:5,
        lineColor:'#579549',
        prop:{
            0:{link:'catalogs .html',color:'#7a5967',text:'Catalogs',
            subLink:{0:{link:'SEO_SMM.html',color:'#4e4b69',text:'SEO/SMM',align:'top'},1:{link:'site_analytics.html',color:'#545454',text:'Site analytics',align:'btm'}}},
            1:{link:'socialmedia.html',color:'#1e9ead',text:'Innovation'},
            2:{link:'loyalty .html',color:'#8fad34',text:'Ideation'},
            3:{link:'promotions .html',color:'#563b64',text:'Promotions'},
            4:{link:'implementations.html',color:'#2c6566',text:'Implementations',
            subLink:{0:{link:'integrating.html',color:'#4c4a66',text:'Integrating',align:'top'},1:{link:'payment.html',color:'#948048',text:'Payment',align:'btm'}}}
            }
    }
}

var object = xploreMaps[id].prop || 'i need subLink';

我的功能:

    $.each(object, function (n,d) {

                var Color = this.color,link = this.link,text=this.text,**subLink** = this.subLink || '';

   // if the sublink there, i need to put back to each, and do the same stuff, what i do for it's parent object.

                var myTimeout = setTimeout(function(){
                    redSubCircles.push(paper.path("M"+x +" "+y)
                    .attr({stroke:brdColor})
                    .animate({path:"M"+(x-((stroke/2)+(n*((radious*2)+stroke+line)))) +" "+y+'l'+(-line)+' 0'},1000,
                        function(){
                            var c =    paper.circle((x-(radious+stroke+line) - n*((radious*2)+stroke+line)),y,radious)
                            .attr({
                                stroke:strokeCl,
                                'stroke-width':stroke,
                                opacity:opacity,
                                fill:Color,
                                href:link
                            });

                            var p =    paper.text((x-(radious+stroke+line) - n*((radious*2)+stroke+line)),y,text)
                            .attr({fill:strokeCl,'font-size':fSize,href:link})

                            redSubCircles.push(c,p)//push to set;-

                        }));
                },1000*n)
            } )

如果我错了,我很抱歉。请告诉我正确的方法。

4

1 回答 1

1

也许一个while循环和一个临时数组会帮助你。

将您的初始项目添加到临时数组中 当临时数组中有项目时调用该函数

每次遇到子链接时,将其推送到您正在迭代的临时数组中。临时数组变大,并继续运行。

每次你完成一个项目,删除它。临时数组变小,最终达到零 - 完成你的循环。

您将需要 while、push 和 splice 从阵列中删除项目。

这是一个独立的例子,这里有一个演示

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>looper</h1>
    <script type="text/javascript">
    function ObjectMaker(identifier) {
        var that = {
          subObj:null,
          name: identifier,
          getName: function() {
            return "I am "+this.name;
          },
          setSubObj: function(sub) {
            this.subObj = sub ;
          }       
        }
        return that;
    }
    var twoAndAHalf = ObjectMaker("two and  a half");
    var two = ObjectMaker("two");
    two.setSubObj(twoAndAHalf);
    var items = [ObjectMaker("one"),two,ObjectMaker("three")];
    while(items.length>0) {
        if (items[0].subObj != null) {
            items.push(items[0].subObj);
        }
        alert(items[0].getName());
        items.splice(0,1);
    }
    </script>
</body>
</html>
于 2012-11-08T12:54:58.843 回答