1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
let com = {
1: {
id_comment: 108,
id_pub: 96
}
,0: {
id_comment: 107,
id_pub: 96
}
,sous_commentaire: [{
id_details_comment: 48,
id_comment: 107
},
{
id_details_comment: 38,
id_comment: 100
},
{
id_details_comment: 50,
id_comment: 107
},
{
id_details_comment: 49,
id_comment: 107
}],
sous_sous_comment: [
{
id_2s_com:2
,id_details_comment:48
},{
id_2s_com:3
,id_details_comment:48
},{
id_2s_com:20
,id_details_comment:18
}],
"3s_comment": []
},t=[],_index,compteur,compteur_s,compteur_2s
,supprimer_com=function(id){
t.length=0;
compteur=0,compteur_s=0,compteur_2s=0
$.map(com, function(val, cle) {
if (!isNaN(cle)) {
if(val.id_comment==id){
t.push({type:'com',name_cle:cle
,id:val.id_comment});
com.sous_commentaire.map(function(cle1, i) {
if (cle1.id_comment == val.id_comment) {
compteur_s++;
t.push({type:'s_com',index:i
,nbr_removed:i==0?i+1:compteur_s
,id:cle1.id_details_comment});
com.sous_sous_comment.map(function(cle2,i2){
if(cle2.id_details_comment==cle1.id_details_comment){
compteur_2s++;
t.push({type:'2s_com',index:i2
,nbr_removed:i2==0?i2+1:compteur_2s
,id:cle2.id_2s_com});
}
});
}
});
}
}
});
//parcourir t pour effectuer la suppression :
$.map(t,function(val_cle,nom_cle){
if(val_cle.type=='com'){
delete com[nom_cle];
}
else if(val_cle.type=='s_com'||val_cle.type=='2s_com'){
if(val_cle.index==0){
_index=0;
}
else if(val_cle.index>0){
if(val_cle.index+1==val_cle.nbr_removed||val_cle.index==val_cle.nbr_removed){
_index=val_cle.index-parseInt(val_cle.nbr_removed-1);
}
else if(val_cle.index>val_cle.nbr_removed){
_index=parseInt(val_cle.index-val_cle.nbr_removed);
}
}
console.log('index :'+_index);
if(val_cle.type=='s_com'){com.sous_commentaire.splice(_index,1);}
else if(val_cle.type=='2s_com'){com.sous_sous_comment.splice(_index,1);}
}
});
console.log(JSON.stringify(com));
} |