473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

hierarchy

i need a script that will take a collection of strings eg:
inbox
inbox/folder-1
inbox/folder-1/Subfolder1
trash
sent and turns it into

root <static, added elsewhere
|_inbox
| |_folder-1
| |_subfolder-1
|_sent
Jul 23 '05 #1
1 1291
Christopher A. Kelly wrote:
i need a script that will take a collection of strings eg:
inbox
inbox/folder-1
inbox/folder-1/Subfolder1
trash
sent and turns it into

root <static, added elsewhere
|_inbox
| |_folder-1
| |_subfolder-1
|_sent


Quick hack:

/**
* Allows for setting a property of an property.
*
* @author
* (C) 2004 Thomas Lahn <ob*******@PointedEars.de>
* Distributed under the terms of the GNU GPL v2.
* @argument object oRoot
* Reference to the object which owns the property
* which property's value should be set.
* @argument string sPropertyPath
* @{(value)}-delimited path of property names.
* @optional _ value = undefined
* Value to assign to the nested property.
* @optional string|RegExp delimiter = "/"
* Delimiter used to split @{(sPropertyPath)} into
* path components.
*/
function setNestedProperty(oRoot, sPropertyPath, value, delimiter)
{
if (!delimiter)
{
delimiter = "/";
}

var aPropertyPath = sPropertyPath.split(delimiter);
for (var i = 0, len = aPropertyPath.length - 1; i < len; i++)
{
var
sComponent = aPropertyPath[i],
t, m = o.hasOwnProperty,
bMethod = ((t = typeof m) == "function"
|| (t == "object" && m));

if ((bMethod && !o.hasOwnProperty(sComponent)
|| (!bMethod && o[sComponent] == "undefined"))
{
oRoot[sComponent] = {};
}

oRoot = oRoot[sComponent];
}

oRoot[aPropertyPath[len]] = value;
}

var oTree = {};
for (var aTree = ['inbox', 'inbox/folder-1', 'inbox/folder-1/Subfolder1',
'trash'],
i = 0,
len = aTree.length;
i < len;
i++)
{
setNestedProperty(oTree, aTree[i].split('/').join('/ '), {});
}

This should create an object with the following structure:

var oTree = {
' inbox': {
' folder-1': {
' subfolder-1': {}
}
},
' sent': {}
}

The leading space should prevent built-in prototype properties from being
accidentally overlayed. Can be optimized if only the leaves are iterated
as iterating a leaf will automagically create its superordinate nodes.

Displaying this object as a tree is left as an exercise to the reader.
PointedEars
--
This isn't Mission Difficult, Mr. Hunt, it's Mission Impossible
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
1
by: Chris Lasher | last post by:
Hello, I'm trying to write a tool to scrape through some of the Ribosomal Database Project II's (http://rdp.cme.msu.edu/) pages, specifically, through the Hierarchy Browser....
0
by: archway | last post by:
Hi, I would like to build a hierarchy of ProductNode objects like so ProductNode ---Product Node ---ProductNode ------ProductNode ------ProductNode ---ProductNode
2
by: Matt | last post by:
Hello, I would like to generate what I call an "overall class hierarchy" in UML automatically derived from my C++ code source. An example of what I seek: ...
2
by: Yaro | last post by:
Hi All (UDB 8.1 FP8, Win) In my simple test database, I have two tables CREATE TABLE "DB2ADMIN"."AAA" ( "F1" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY, "F2" VARCHAR(10), PRIMARY KEY...
4
by: Dave Veeneman | last post by:
I have an object hierarchy that's several layers deep, and I'm puzzling over how to access an object at any point in the hierarchy, solely by its key. The hierarchy is similar to an...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
2
by: Do | last post by:
Hi, I have a database table with the following fields: id, name, parentid. These fields are supposed to create a hierarchy for a list box, an infinite hierarchy Child fields of parent fields...
10
by: John Nagle | last post by:
Here are three network-related exceptions. These were caught by "except" with no exception type, because none of the more specific exceptions matched. This is what a traceback produced: 1....
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.