I wanna access the properties and their values of an object dynamically, the scenario is:
const SoldeN = {
1:4,
}
const SoldeN_1 = {
2:5,
}
const soldes = [
{ formula: '=SoldeN("1")',value:0},
{ formula: '=SoldeN_1("2")',value:0},
{ formula: '=SoldeN_1("1")+SoldeN_1("2")',value:0},
];
What I've tried so far:
const getProperty = string => string.match(/\(.*?\)/g).map(x => x.replace(/[(")]/g, ''))
const getObject = string => string.match(/[=+](.*?)\(/g).map(x => x.replace(/[=(+]/g, ''))
console.log(soldes.map(solde=>[getObject(solde.formula),getProperty(solde.formula)]))
[
[["SoldeN"], ["1"]],
[["SoldeN_1"], ["2"]],
[
["SoldeN_1", "SoldeN_1"],
["1", "2"],
],
];
Till now Everything is ok, but To get the value of an object dynamically based on property I used this function:
const getPropertyValue = (obj, prop) => obj[prop];
getPropertyValue('SoldeN','1')
//'o'
It gives me 'o' instead of 1, I know If I've passed the reference of an object it bring its value; but to get it dynamically I've to pass the actual name of an object which is string, not the object reference.
The expected Result would be:
result = [
{ formula: '=SoldeN("1")',value:4},
{ formula: '=SoldeN_1("2")',value:5},
{ formula: '=SoldeN_1("1")+SoldeN_1("2")',value:9},
];