UnderscoreJs,Array迭代问题..返回多个结果

时间:2014-05-16 05:09:34

标签: javascript jquery underscore.js arrayiterator

我使用下划线数组迭代器来操作对象。结果应该是单个时间返回,但我得到多次,就像我的数组长度一样。怎么纠正这个?

这是我的功能:

var ValidPaths = {
        "green":{
            "url":"greenController.js",
            "lightgreen" :{
                "url":"lightGreenController.js",
                "floragreen":{
                    "url":"floragreenController.js",
                    "ultragreen":{
                        "url":"floragreenController.js",
                    }
                }
            }
        }
    }

var path = "green",
    subPath = 'lightgreen/floragreen';

var paths = [path].concat(subPath.split('/')),
    arr = [],prePath=ValidPaths;

             function pathFinder (path,i){
                if(prePath[path]){
                    arr.push({"label":path,"url":prePath[path]['url']});
                    prePath = prePath[path];
                } else{
                    console.log("there is no path like " + path + "in ValidPaths");
                }
                return arr;
            }

            var breadCrumb = _.map(paths, function(root,i){
                return pathFinder(root,i);
            });

            console.log("breadCrumb", breadCrumb); // giving me 3 arrays instead 1 how to solve this?

Here is the Live Demo

0 个答案:

没有答案