am trying to make JSON content dynamic and the idea is to have a simple json content
{
"name": "@@var1@@"
"lastname":"@@var2@@"
}
and have another json content
{
"var1": "samy"
"var2":"something"
}
the idea is to use the second JSON content to fill in the variables in the first JSON and become
{
"name": "samy"
"lastname":"something"
}
I did search for a library to help me achieve that and I did file json-variables but unfortunately, it doesn't work the way I want instead it does use variables from the same JSON file
const jv = require("json-variables");
var res = jVar({
a: "some text %%_var1_%% more text %%_var2_%%",
b: "something",
var1: "value1",
var2: "value2",
});
console.log("res = " + JSON.stringify(res, null, 4));
// ==> {
// a: 'some text value1 more text value2',
// b: 'something',
// var1: 'value1',
// var2: 'value2'
// }
const arr1 = {
"name": "@@var1@@",
"lastname":"@@var2@@"
}
const arr2 = {
"var1": "samy",
"var2":"something"
}
function myFunction(arr1, arr2) {
return Object.entries(arr1).reduce((acc, [key, value]) => {
acc[key] = arr2[value.replace(/@/g, '')];
return acc;
}
, {})
}
console.log(myFunction(arr1, arr2));