-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc-jsonFindReplace.ts
60 lines (53 loc) · 1.49 KB
/
cc-jsonFindReplace.ts
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
export class FindReplace {
constructor() {}
public static replaceByValue(object: any, value: string, replaceValue: string): void {
for (const key in object) {
if (typeof object[key] === typeof {}) {
this.replaceByValue(object[key], value, replaceValue);
}
if (object[key] === value) {
object[key] = replaceValue;
}
}
}
public static replaceByProperty(object: any, prop: string, replaceValue: string): void {
for (const key in object) {
if (typeof object[key] === typeof {}) {
this.replaceByProperty(object[key], prop, replaceValue);
}
if (key === prop) {
object[key] = replaceValue;
}
}
}
public static replaceByPropertyAndValue(object: any, prop: string, value: string, replaceValue: string): void {
for (const key in object) {
if (typeof object[key] === typeof {}) {
this.replaceByPropertyAndValue(object[key], prop, value, replaceValue);
}
if (object[key] === value) {
object[prop] = replaceValue;
}
}
}
}
/**
* Example:
*
* FindReplace.replaceByValue(object, 'Moers', 'REPLACED VALUE');
* FindReplace.replaceByProperty(object, 'city', 'REPLACED VALUE');
* FindReplace.replaceByPropertyAndValue(object, 'city', 'Moers', 'REPLACED VALUE');
*
* let object = [
{ "plz": 47441,
"city": "Moers",
"lattitude": 51.4463,
"longitude": 6.6396
},
{ "plz": 47798,
"city": "Krefeld",
"lattitude": 51.3311,
"longitude": 6.5616
}
];
*/