获取对象树的路径

时间:2015-02-06 19:50:42

标签: javascript

编辑:我在这里更新了数据结构和新测试:http://jsfiddle.net/6Lwrsjou/5/ images2正在嵌套在图像下,它不应该是。

我有一个包含这样的对象的数组:

var objects = [{
    _id: 1,
    name: 'images',
    type: 'directory',
    children: [{
        _id: 2,
        name: 'set 2',
        type: 'directory',
        children: [{
            _id: 3,
            name: 'image.jpg',
            type: 'file'
        },
        {
            _id: 4,
            name: 'image2.jpg',
            type: 'file'
        },
        {
        _id: 5,
        name: 'set 3',
        type: 'directory',
        children: [{
            _id: 6,
            name: 'image.jpg',
            type: 'file'
        },
        {
            _id: 7,
            name: 'image2.jpg',
            type: 'file'
        }]

      }]

    }]

}]

我想要做的是基于_id值,使用name值获取该对象的路径。

例如,对于_id: 6,路径为images/set 3/

对于我尝试的内容,我有一个小提琴http://jsfiddle.net/6Lwrsjou/2/,但这不起作用,它包括以前不是父母的集合。

var path = '';
function getDirectory(objects, id) {
    _.each(objects, function(item) {
        if (item._id == id) return false;
        if (item.type === 'directory') {
            if (path.length > 1) {
                path += '/' + item.name;
            } else {
                path += item.name;
            }
        };
        if (!_.isEmpty(item.children)) {
            getDirectory(item.children, id);
        }
    });
}
getDirectory(objects, 7);
console.log(path);

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您需要对代码进行一些更改,以便在所有对象中查找,例如

var objects = [{
	_id: 1,
	name: 'images',
	type: 'directory',
	children: [{
		_id: 2,
		name: 'set 2',
		type: 'directory',
		children: [{
			_id: 3,
			name: 'image.jpg',
			type: 'file'
		},
		{
			_id: 4,
			name: 'image2.jpg',
			type: 'file'
		},
        {
		_id: 5,
		name: 'set 3',
		type: 'directory',
		children: [{
			_id: 6,
			name: 'image.jpg',
			type: 'file'
		},
		{
			_id: 7,
			name: 'image2.jpg',
			type: 'file'
		}]
      
      }]
        
    }]
    
},{
    _id: '1a',
	name: 'images2',
    type: 'directory',
    children: [{
			_id: '2a',
			name: 'image2.jpg',
			type: 'file'
    }]
}]


function gd(arr, id, p){
    var i,len,j, childlen;
    console.log('gd:'+p);
    for(i=0, len=arr.length; i<len;i++){
        if(arr[i]._id == id) return p+'/'+ arr[i].name;
        if(arr[i].children && arr[i].children.length >0){
            var f = gd(arr[i].children,id,p+'/'+arr[i].name)
            if(f) return f;
        }
    }
}

document.getElementById('result').innerHTML = gd(objects, '2a','');
<span id="result"></span>

答案 1 :(得分:0)

var objects = [{
    _id: 1,
    name: 'images',
    type: 'directory',
    children: [{
        _id: 2,
        name: 'set 2',
        type: 'directory',
        children: [{
            _id: 3,
            name: 'image.jpg',
            type: 'file'
        },
        {
            _id: 4,
            name: 'image2.jpg',
            type: 'file'
        },
        {
        _id: 5,
        name: 'set 3',
        type: 'directory',
        children: [{
            _id: 6,
            name: 'image.jpg',
            type: 'file'
        },
        {
            _id: 7,
            name: 'image2.jpg',
            type: 'file'
        }]

      }]

    }]

},{
    _id: '1a',
    name: 'images2',
    type: 'directory',
    children: [{
            _id: '2a',
            name: 'image2.jpg',
            type: 'file'
    }]
}]


function getDirectory(object, id){
    var path="";
    for(var i=0; i<object.length; i++){
        if(object[i]._id == id) return object[i].name;
        else{
            if(typeof(object[i].children) !== "undefined"){
                temp = getDirectory(object[i].children, id);
                if(temp){
                    path += object[i].name+"/" + getDirectory(object[i].children, id);
                    return path;
                }
            }
        }
    }
    return false;
}

path = getDirectory(objects, "6");
console.log(path);