I'm trying to figure out how to replace multiple object values using a key-value table.
I've been trying to learn from functions that do this on regular strings, and make it work for objects (specifically object values). I thought maybe there could be a way to merge this (https://stackoverflow.com/a/15604206/2458671) kind of function with my replaceValue function below.
var settings = {
pushItems: {
"[make_up_policy_attributes][staff_allowed]": true,
"[make_up_policy_attributes][default_issued_plan_product_id]": "{RPPA}",
"[make_up_policy_attributes][issued_plan_product_expires]": true,
"[make_up_policy_attributes][issued_plan_product_duration_months]": 0,
"[make_up_policy_attributes][issued_plan_product_duration_weeks]": 0,
"[make_up_policy_attributes][issued_plan_product_duration_days]": 30
}
}
replaceValue(settings.pushItems, "{RPPA}", 12345 );
function replaceValue(obj, findvalue, replacewith) {
for (var key in obj) {
var value = obj[key];
if (value === findvalue) {
obj[key] = value.replace(findvalue, replacewith);
}
}
}
Basically I want to use an object { searchForValue1: replaceWithValue1, searchForValue2: replaceWithValue2 } so I can substitute multiple things if they are found in the settings.pushItems object.