I have an unknown javascript object (let's call it IncompleteObject just for readability) and an array of IVariables which can be anything, but in the following format:
key: string
value: unknown
Example:
IVariables:
[
{ key: 'someObject', value: { some:'value' },
{ key: 'name', value: 'another value' },
{ key: 'lastName', value: 'this variable exists but wont be used' }
]
IncompleteObject:
{
ID: "SGML",
SortAs: "{{someObject}}",
GlossTerm: "Standard Generalized Markup Language",
Acronym: "The acronym is {{name}}",
GlossSee: "markup"
}
Expected Result:
{
ID: "SGML",
SortAs: {
some:'value'
},
GlossTerm: "Standard Generalized Markup Language",
Acronym: "The acronym is another value",
GlossSee: "markup"
}
The solution i thought was stringfying the object, replacing everything as strings and then trying to parse as JSON again (if it fails, it fails), but i'm wondering if there is a better solution to this... Also idk how to make it so that the SortAs for example becomes an object and not a string
Thanks!
*Notes:
- The object won't necessarily have all variables, like the example.
- The {{}} format is just an idea, i have no problem changing it since {} is used in JSON
SortAs: "hello {{someObject}} world"- is the whole value changed again? Is the object then inserted into the string? What should that produce? Similar problems if you have other non-strings as replacement values.{ key: "<someObject>", value: { some:'value' } }will be known from the get-go that it will change the entire value. It might even be{ key: "<SortAs>", value: { some:'value' } }(matching the key where the value would be replaced) or maybe even{ fullReplace: "SortAs", value: { some:'value' } }(to more directly identify the whole value would be changed). Might even be{ key: "{{someObject}}", value: { some:'value' }, full: true }