I am trying to create an array of category objects from an array of materialized category paths.
var data = [
    'Business / Finance',
    'Business / Management',
    'Business / Management / Leadership',
    'Business / Team / Leadership'
];
// Expected results:
var result = [
    { name: 'Business', trail: null, path: 'Business' },
    { name: 'Finance', trail: 'Business', path: 'Business / Finance' }, 
    { name: 'Management', trail: 'Business', path: 'Business / Management' },
    { name: 'Leadership', trail: 'Business / Management', path: 'Business / Management / Leadership' }, 
    { name: 'Team', trail: 'Business', path: 'Business / Team / Leadership' },
    { name: 'Leadership', trail: 'Business / Team', path: 'Business / Team / Leadership' }
];
As you can see, Business should only be present once, since all others are only subcategories. However, Leadership should be present twice, because both are in a different structure.
When you check out the fiddle http://jsfiddle.net/9uC9Z/ you can see that Business exists 4 times.
How can I solve the problem?
I would really appreciate code comments if the resulting code is very complex.
Edit:
The materialized path strings in the data array reflect category hierarchies for books. An example would be:
{
    title: 'Leadership 101',
    author: 'John Smith',
    category: 'Business / Management / Leadership'
}
That just represents one book. I now want to create one MongoDB document for every category. Above sample book would produce three category objects (Business, Management, Leadership). However, if a category (or a subcategory) object/document already exists, I don't need to create another one.
resulttherefore represents the category objects I will store inside my MongoDB collection. (I will add relationships between the categories, but that isn't part of the current problem.)
 
     
    