我有一个嵌套对象数组(我用它来填充我的树),如下所示..
var obj1= {
text: "TreeRoot",
items: [ {
text: "subgroup1" ,
items: [ {
text: "subgroup2",
items: [ {
text: "subgroup3",
items: [ {
text: "subgroup4",
items: [ {
text: "subgroup5"
}]
}]
}]
}]
}]
};
var obj2 = {
text: "TreeRoot",
items: [ {
text: "subgroup1" ,
items: [ {
text: "subgroup2",
items: [ {
text: "subgroup3",
items: [ {
text: "subgroup4",
items: [ {
text: "subgroup5"
}]
}]
}]
}]
}]
};
var obj3= {
text: "TreeRoot",
items: [ {
text: "subgroup1" ,
items: [ {
text: "subgroup2"
}]
}]
}]
};
var finalArray=[];
finalArray.push(obj1);
finalArray.push(obj2);
finalArray.push(obj3);
现在我需要使用文本从我的最终数组中删除重复的对象(即我应该从我的数组中删除 obj2)...
这是我试过的..
var arr = {};
for ( var i=0; i < finalPath.length; i++ )
arr[finalArray[i]['text']] = finalArray[i];
finalArray= new Array();
for ( key in arr )
finalArray.push(arr[key]);
有人可以让我知道最好的方法吗?
编辑:
我认为以下解决方案有效,但尚未完全测试...
var arr = {};
for ( var i=0; i < finalArray.length; i++ ){
if(finalArray[i].items){
for(var j=0;j<finalArray[i].items.length;j++){
arr[finalArray[i].items[j]['text']] = finalArray[i];
}
}else{
arr[finalArray[i]['text']] = finalArray[i];
}
}
finalArray= new Array();
for ( key in arr )
finalArray.push(arr[key])
谢谢, 巴拉尼