-
Notifications
You must be signed in to change notification settings - Fork 1
/
递归字符串a.b.c生成对象.js
138 lines (124 loc) · 2.83 KB
/
递归字符串a.b.c生成对象.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const cutString = (str, value) => {
const idx = str.lastIndexOf(".");
if (idx > -1) {
const prefix = str.substring(0, idx);
const key = str.substring(idx + 1);
const tmp = {
[key]: value,
};
return cutString(prefix, tmp);
} else {
return {
[str]: value,
};
}
};
const ob = {
"a.b.c.d": "perfectyang",
"a.b.d": "same",
"a.d.xx": "ehheje",
"b.e": "ae",
};
const readList = (ob) => {
// const arr = []
const newOb = [];
Object.keys(ob).forEach((key) => {
newOb.push(cutString(key, ob[key]));
});
return newOb;
};
// const demo = "a.b.c.d.e.f";
// const ret = cutString(demo, "perfectyang");
// console.log(JSON.stringify(ret, 2));
class GenerateOb {
constructor() {
this.node = {};
}
insert(word, value) {
let node = this.node;
const len = word.length - 1;
word.forEach((w, idx) => {
if (!node[w]) {
node[w] = {};
}
if (idx === len) {
node[w] = value;
} else {
node = node[w];
}
});
}
}
const strTries = new GenerateOb();
const obTest = {
"a.b.c.d": "perfectyang",
"a.b.c.f": "perfectyang",
"a.b.d": "same",
"a.d.xx": "ehheje",
"b.e": "ae",
};
const generate = (ob) => {
Object.keys(ob).forEach((cur) => {
const str = cur.split(".");
strTries.insert(str, ob[cur]);
});
};
generate(obTest);
console.log(JSON.stringify(strTries.node, 2));
// ------------根据路径生成对象数组
function isObject(value) {
const type = typeof value;
return value != null && (type === "object" || type === "function");
}
const MAX_SAFE_INTEGER = 9007199254740991;
const reIsUint = /^(?:0|[1-9]\d*)$/;
function isIndex(value, length) {
const type = typeof value;
length = length == null ? MAX_SAFE_INTEGER : length;
return (
!!length &&
(type === "number" || (type !== "symbol" && reIsUint.test(value))) &&
value > -1 &&
value % 1 == 0 &&
value < length
);
}
// {} 'a.b[0].c' 1
function baseSet(object, _path, value) {
const path = _path.split("."); // ['a', 'b', '0', 'c']
const length = path.length;
const lastIndex = length - 1;
let index = -1;
let nested = object; // {}
while (nested != null && ++index < length) {
const key = path[index];
let newValue = value;
if (index != lastIndex) {
const objValue = nested[key];
newValue = undefined;
if (newValue === undefined) {
newValue = isObject(objValue)
? objValue
: isIndex(path[index + 1])
? []
: {};
}
}
nested[key] = newValue;
nested = nested[key];
}
return object;
}
const result = [];
const info = {
"0.b.0.c": 1,
"0.b.0.d": 33,
"0.b.1.c": 1,
"1.b.0.c": 1,
"1.b.0.d": 33,
"1.b.1.c": 1,
};
Object.entries(info).forEach(([key, value]) => {
baseSet(result, key, value);
});
console.log(JSON.stringify(result));