-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
111 lines (104 loc) · 2.15 KB
/
Copy pathserver.js
File metadata and controls
111 lines (104 loc) · 2.15 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var data = {
en: {
"201:en 201": {
"2001:en 2001": {
"detail": {
"all": "first"
}
}
},
"202:en 202":{
"2002:en 2002": {
"detail": {
"all": "second"
}
}
}
},
fa: {
"201:fa 201": {
"2001:fa 2001": {
"detail": {
"all": "الف"
}
}
},
"202:fa 202":{
"2002:fa 2002": {
"detail": {
"all": "دوم"
}
}
}
}
};
var newObj1 = {};
var createObj = function(cat, subcat){
if(!subcat){
if(!newObj1[cat[0]]){
newObj1[cat[0]]= {"en":cat[1]};
}else{
newObj1[cat[0]].fa = cat[1];
}
}else{
if(!newObj1[cat[0]])
newObj1[cat[0]] = {};
if(!newObj1[cat[0]][subcat[0]]){
newObj1[cat[0]][subcat[0]]= {"en":subcat[1]};
}else{
newObj1[cat[0]][subcat[0]].fa = subcat[1];
}
}
}
Object.keys(data).map(function(dataKey, dataIndex){
Object.keys(data[dataKey]).map(function(catProp, catIndex){
var splitedDataCat = catProp.split(':');
createObj(splitedDataCat);
Object.keys(data[dataKey][catProp]).map(function(subCatProp, subCatId){
var splitedSubCatData = subCatProp.split(':');
createObj(splitedDataCat,splitedSubCatData)
newObj1[splitedDataCat[0]][splitedSubCatData[0]].detail = JSON.stringify(data[dataKey][catProp][subCatProp]);
})
})
});
console.log("*****************************");
// var newObj2 = {};
// var traverse = function(obj){
// for(var prop in obj){
// if(prop!=="detail"){
// if(prop!='en'&&prop!='fa'){
// var splitedData = prop.split(':');
// if(!newObj2[splitedData[0]]){
// newObj2[splitedData[0]]= {"en":splitedData[1]};
// }else{
// newObj2[splitedData[0]].fa = splitedData[1];
// }
// }
// traverse(obj[prop])
// }else{
// // console.log(obj[prop]);
// }
// }
// }
// traverse(data);
console.log(newObj1);
// {
// '201': {
// '2001': {
// en: 'en 2001',
// detail: '{"detail":{"all":"الف"}}',
// fa: 'fa 2001'
// },
// en: 'en 201',
// fa: 'fa 201'
// },
// '202': {
// '2002': {
// en: 'en 2002',
// detail: '{"detail":{"all":"دوم"}}',
// fa: 'fa 2002'
// },
// en: 'en 202',
// fa: 'fa 202'
// }
// }