5 examples of 'javascript replace object value' in JavaScript

Every line of 'javascript replace object value' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
129function doReplace(obj, paramName, newValue) {
130 if (obj.hasOwnProperty(paramName)) {
131 console.log("Replaced: " + obj[paramName] + " with " + newValue);
132 obj[paramName] = newValue;
133 }
134};
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

148function replace(inObj, path, value) {
149 if (inObj == null) {
150 return;
151 }
152 var q = inObj, t = q;
153 path.replace(/([^\.])+/g, function (term, lc, pos, str) {
154 var array = term.match(/([^\[0-9]+){1}(\[)([0-9+])/), last = pos + term.length >= str.length, _getArray = function () {
155 return t[array[1]] || (function () {
156 t[array[1]] = [];
157 return t[array[1]];
158 })();
159 };
160 if (last) {
161 // set term = value on current t, creating term as array if necessary.
162 if (array) {
163 _getArray()[array[3]] = value;
164 }
165 else {
166 t[term] = value;
167 }
168 }
169 else {
170 // set to current t[term], creating t[term] if necessary.
171 if (array) {
172 var a_1 = _getArray();
173 t = a_1[array[3]] || (function () {
174 a_1[array[3]] = {};
175 return a_1[array[3]];
176 })();
177 }
178 else {
179 t = t[term] || (function () {
180 t[term] = {};
181 return t[term];
182 })();
183 }
184 }
185 return "";
186 });
187 return inObj;
188}
108function replaceValue(startingString, replaceString, instance) {
109 let outputValue = startingString;
110 let replacementVal;
111 const replaceField = replaceString.replace('${', '')
112 .replace('}', '').split('.');
113 if (replaceField.length > TWO) {
114 try {
115 replacementVal = JSON.parse(instance.value)[replaceField[TWO]];
116 } catch (e) {
117 return false;
118 }
119
120 if (isJson(outputValue)) { // Output value is a serialized string
121 let outputValueObj;
122 try {
123 outputValueObj = JSON.parse(outputValue);
124 } catch (e) {
125 return false;
126 }
127
128 for (const property in outputValueObj) {
129 if (outputValueObj.hasOwnProperty(property) &&
130 outputValueObj[property].includes(replaceString)) {
131 outputValueObj[property] =
132 outputValueObj[property].replace(replaceString, replacementVal);
133 }
134 }
135
136 outputValue = serialize(outputValueObj);
137 } else { // Output value is just a string
138 outputValue = outputValue.replace(replaceString, replacementVal);
139 }
140 } else {
141 outputValue = outputValue
142 .replace(replaceString, instance.value);
143 }
144
145 return outputValue;
146} // replaceValue
23function object_key_replace(search, replace, object) {
24 for(var k in object) {
25 log(k);
26 }
27 // $.each(object, (k, v) {
28 // if(k == search) {
29 // object[k] = replace;
30 // }
31 // });
32 return object;
33}
197function replaceObjectTag(obj, replacement) {
198 var ancestor = null;
199 switch(obj.localName) {
200 case 'embed':
201 ancestor = obj.closest('object');
202 break;
203 case 'object':
204 ancestor = obj.closest('embed');
205 break;
206 default:
207 break;
208 }
209 if (ancestor) {
210 obj = ancestor;
211 }
212 obj.parentNode.insertBefore(replacement, obj);
213 obj.parentNode.removeChild(obj);
214}

Related snippets