-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.html
92 lines (81 loc) · 2.32 KB
/
merge.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>i18nExcel2Json</title>
<script src="https://cdn.sheetjs.com/xlsx-0.20.1/package/dist/xlsx.full.min.js"></script>
<style>
.result {
display: flex;
}
textarea {
flex: 1;
height: 800px;
}
* {
user-select: none;
}
pre {
user-select: all;
}
.txt {
display: flex;
}
.txt div {
flex: 1;
}
</style>
</head>
<body>
<button id="btn">合并</button>
<button id="btn2">复制结果</button>
<a href="./index.html" target="_blank">跳转:excel2json</a>
<div class="txt">
<div>原JSON:</div>
<div>新增JSON:</div>
<div>结果JSON:</div>
</div>
<div class="result">
<textarea name="" id="j1"></textarea>
<textarea name="" id="j2"></textarea>
<textarea name="" id="result"></textarea>
</div>
<script>
function isObject(item) {
return item && typeof item === 'object' && !Array.isArray(item);
}
function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return mergeDeep(target, ...sources);
}
const j1 = document.querySelector('#j1')
const j2 = document.querySelector('#j2')
const result = document.querySelector('#result')
document.querySelector('#btn').addEventListener('click', () => {
let json1 = JSON.parse(j1.value)
let json2 = JSON.parse(j2.value)
result.value = JSON.stringify(mergeDeep(json1, json2), null, 2)
navigator.clipboard.writeText(result.value)
})
document.querySelector('#btn2').addEventListener('click', () => {
try {
navigator.clipboard.writeText(result.value)
} catch (error) {
alert('复制失败,请手动复制')
}
})
</script>
</body>
</html>