|
20 | 20 | <script src="sample.js"></script>
|
21 | 21 | <script type="text/javascript">
|
22 | 22 |
|
| 23 | + let doc = { |
| 24 | + "_id" : "5805dfa519f972b200ea2955", |
| 25 | + "s" : { |
| 26 | + "id" : 36435 |
| 27 | + }, |
| 28 | + "a" : [ |
| 29 | + { |
| 30 | + "XX-(random value)" : { |
| 31 | + "V" : 4 |
| 32 | + }, |
| 33 | + "V" : 4, |
| 34 | + "u" : { |
| 35 | + "YY-(random value)" : { |
| 36 | + "V" : 4, |
| 37 | + "ZZ-(random value)" : { |
| 38 | + "V" : 4, |
| 39 | + "WW-(random value)" : { |
| 40 | + "V" : 4 |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + ] |
| 47 | + }; |
| 48 | + |
| 49 | + function checkProperties(doc, key, value) { |
| 50 | + for (var propertyName in doc) { |
| 51 | + //console.log(propertyName); |
| 52 | + if (typeof doc[propertyName] == "object") { |
| 53 | + doc[propertyName] = checkProperties(doc[propertyName], key, value); |
| 54 | + } else { |
| 55 | + if (propertyName == key) { |
| 56 | + doc[propertyName] = value; |
| 57 | + //console.log(doc[propertyName]); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return doc; |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + function changeValue(obj, key ,value) { |
| 67 | + if (typeof obj === 'object') { |
| 68 | + // iterating over the object using for..in |
| 69 | + for (var keys in obj) { |
| 70 | + //checking if the current value is an object itself |
| 71 | + if (typeof obj[keys] === 'object') { |
| 72 | + // if so then again calling the same function |
| 73 | + changeValue(obj[keys], key ,value) |
| 74 | + } else { |
| 75 | + |
| 76 | + if (keys == key) { |
| 77 | + obj[keys] = value |
| 78 | + console.log(keys); |
| 79 | + //doc[propertyName] = value; |
| 80 | + //console.log(doc[propertyName]); |
| 81 | + } |
| 82 | + // else getting the value and replacing single { with {{ and so on |
| 83 | + /* let keyValue = obj[keys].replace('{', '{{').replace('}', '}}'); |
| 84 | + obj[keys] = keyValue;*/ |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return obj; |
| 89 | + } |
| 90 | + |
| 91 | + function getObjects(obj, key, newVal) { |
| 92 | + var newValue = newVal; |
| 93 | + var objects = []; |
| 94 | + for (var i in obj) { |
| 95 | + if (!obj.hasOwnProperty(i)) continue; |
| 96 | + if (typeof obj[i] == 'object') { |
| 97 | + objects = objects.concat(getObjects(obj[i], key, newValue)); |
| 98 | + } else if (i == key) { |
| 99 | + obj[key] = newValue; |
| 100 | + } |
| 101 | + } |
| 102 | + return obj; |
| 103 | + } |
23 | 104 |
|
24 | 105 | let src = JSON.stringify(JSON.parse(JSON.stringify(original2)));
|
25 | 106 |
|
26 | 107 | console.log('Test if original2 is a correct JSON');
|
27 | 108 | console.log(src);
|
28 | 109 |
|
29 |
| - let re1 = Pattern.sculptJson(src, 'self', null); |
| 110 | +/* let re0 = getObjects(original2, 'Mike {[Gentleman]}', 0); |
| 111 | + console.log('0 Bulk update - Mike {[Gentleman]} : null'); |
| 112 | + console.log(JSON.stringify(re0));*/ |
| 113 | + |
| 114 | + |
| 115 | + let re1 = Pattern.sculptJson(src, 'Mike {[Gentleman]}', null); |
30 | 116 | console.log('Bulk update - Mike {[Gentleman]} : null');
|
31 | 117 | console.log(re1);
|
32 | 118 | JSON.parse(re1)
|
|
0 commit comments