-
Notifications
You must be signed in to change notification settings - Fork 1
/
obj-get-value.js
144 lines (136 loc) · 3.3 KB
/
obj-get-value.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
139
140
141
142
143
144
// import isObject from "lodash/isObject";
// import set from 'lodash/isObject'
var data = [
{
question_name: "在这要要",
option_list: [
{
option_value: "达要要要要",
},
],
get_id: "aaa",
extra_logic: {
name: "aaa",
list_name: [
{
group_name: "在这要",
list: [
{
name: "aaaa",
},
{
name: "aaaa",
options: [
{
txt: "在这要要",
link: {
obj: "达薮薮薮薮工",
},
},
],
},
],
list2: [
{
age: "aaaa",
},
{
test: "aaaa",
},
],
},
],
},
},
];
function gettextsandmap(data, prefix = "") {
let texts = [];
let map = [];
for (const [key, value] of object.entries(data)) {
const name = prefix ? prefix + "." + key : key;
if (typeof value === "object") {
const { texts: stexts, map: smap } = gettextsandmap(value, name);
texts = texts.concat(stexts);
map = map.concat(smap);
} else {
if (value) {
texts.push(value);
map.push(name);
}
}
}
return { texts, map };
}
function getTextsAndMap(data, prefix = "", recordMap = {}) {
let texts = [];
let map = [];
for (const [key, value] of Object.entries(data)) {
const name = prefix ? prefix + "." + key : key;
if (typeof value === "object") {
const { texts: sTexts, map: sMap } = getTextsAndMap(
value,
name,
recordMap,
);
texts = texts.concat(sTexts);
map = map.concat(sMap);
} else {
if (value) {
texts.push(value);
map.push(name);
recordMap[name] = value;
// console.log("name", name, value);
}
}
}
return { texts, map, recordMap };
}
// const { texts, map } = getTextsAndMap(data);
// console.log("直实节点", texts, map);
// function recoveryByMap(src, dist, paths) {
// paths.forEach((curpath, key) => {
// // console.log('src', src) // 原数据
// // console.log('curpath', curpath) // 数据的路径
// // console.log('dist[key]', dist[key]) // 要赋值
// // set(src, curpath, dist[key]); // 所用lodash赋值
// });
// return src;
// }
// recoveryByMap(data, texts, map);
const MapData = {
keyword: {
start: "1",
end: "12",
empty: "",
},
txt: undefined,
text2: "asdf",
filter: [
{
a: "aaa",
b: "bbb",
},
],
text: 0,
};
const { texts, map, recordMap: hashMap } = getTextsAndMap(MapData);
console.log(hashMap);
// 过滤对象或组数的中指定类型
const judageType = (val) =>
val === undefined || val === "" || (Array.isArray(val) && val.length === 0);
const compObject = (ob) => {
const val = Array.isArray(ob) ? ob.filter(Boolean) : ob;
return Object.keys(val).reduce(
(acc, key) => {
const value = val[key];
console.log("value", value, String(value) !== "[]");
if (!judageType(value)) {
acc[key] = typeof value === "object" ? compObject(value) : value;
}
return acc;
},
Array.isArray(val) ? [] : {},
);
};
const ret = compObject(MapData);
// console.log("ret", ret);