Reputation: 1189
I tried to format a array to object. it's working fine but I need to know, Is this correct or wrong? any other standard way to do this?.
I used underscoreJS for this.
//What I get from API
"A/a/1" = A -> Category, a -> Subcategory, 1 -> child of subcategory.
["A", "A/a", "A/b", "A/a/1", "A/a/2", "B", "B/a"];
//Which format i need.
[
{
"name":"A",
"parent":"",
"children":[
{
"name":"a",
"parent":"A",
"children":[
{
"name":"1",
"parent":"a",
"children":[
]
},
{
"name":"2",
"parent":"a",
"children":[
]
}
]
},
{
"name":"b",
"parent":"A",
"children":[
]
}
]
},
{
"name":"B",
"parent":"",
"children":[
{
"name":"a",
"parent":"B",
"children":[
]
}
]
}
]
MY CODE :
var dataObj = function(){
this.name = "";
this.parent = "";
this.administrator = "";
this.children = [];
};
var d_ = [];
_.each(data, function(item, index){
var row = new dataObj();
var item_array = item.split("/"),
item_array_length = item_array.length;
if(item_array.length == 1){
row.name = item_array[0];
d_.push(row);
} else {
row.name = item_array[1];
row.parent = item_array[0];
var newC = d_[_.findIndex(d_, {name:item_array[0]})];
if(item_array.length == 2) {
newC.children.push(row);
} else if(item_array.length == 3) {
newC.children[_.findIndex(newC.children, {name: item_array[1]})]
.children.push({name : item_array[2], parent : item_array[1]});
}
}
});
UPDATE The level of subcategory is not limited. "A/a/1/i/n/x/y...."
Upvotes: 1
Views: 65
Reputation: 5163
I feel your implementation can be improved. Following are the few possibilities:
["A/b", "A/a/1", "A/a/2", "B/a"]
instead of ["A", "A/a", "A/b", "A/a/1", "A/a/2", "B", "B/a"]
_
library does not seem helping much, the code can be implemented in plain javaScript.A simple algorithm I can think of would consist of the following high level steps:
sampleArray
.sampleArray
we have currentSplittedElementArray = sampleArray[i].split('/');
finalOutput
finalOutput
and test if the element is already present finalOutput[i].name === currentSplittedElementArray[0]
currentSplittedElementArray
and update the same element of the finalOutput
with the elements of currentSplittedElementArray
. (If the element is not already present on the finalOutput
then let's first create the element on the finalOutput
and populate it with the elements of currentSplittedElementArray
)I know the above algorithm may seem daunting and the steps outlined will include invoking a function repeatedly in a loop or using some sort of recursion. But this is something I would have done if I were to implement something like this.
I am open to any area of improvement/optimization in my approach.
Upvotes: 2