dzm
dzm

Reputation: 23534

Get path of an object tree

EDIT: I've updated the data structure and new test here: http://jsfiddle.net/6Lwrsjou/5/ images2 is being nested under images, which it shouldn't be.

I have an array that contains objects like this:

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'
        }]

      }]

    }]

}]

What I want to do is based on the _id value, get a path to that object using the name value.

So for example, for _id: 6 the path would be images/set 3/

I have a fiddle http://jsfiddle.net/6Lwrsjou/2/ for what I've tried, but this doesn't work, it includes previous sets that are not parents.

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

Any ideas?

Upvotes: 0

Views: 541

Answers (2)

Kehan Wang
Kehan Wang

Reputation: 173

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

Upvotes: 0

Grundy
Grundy

Reputation: 13381

You need a little change your code, for find in all objects, something like this

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>

Upvotes: 1

Related Questions