Home > Software engineering >  how to automaticaly get values to set in params
how to automaticaly get values to set in params

Time:10-23

i'm trying to execute external inputs with functions and params, get params values in database and changes this values before execute the functions, i create a exemple below to show my problem

// Values in DB
const InternalValues = {
  phone: '123456789',
  old: '25'
}

//Values from external inputs, when params old=param1 and phone=param2
const externalValues = {
  data: `return function exec(param1,param2){
            console.log("values",param1,param2)
        };`,
  params: ['old', 'phone'],
}

// Create a function
const Func = new Function(externalValues.data)();

//Finding ExternalParams in Internal Values manualy
const param1 = InternalValues[externalValues.params[0]]
const param2 = InternalValues[externalValues.params[1]]

// Execute Function
const ii = Func(param1, param2)
console.log(ii);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

if i put more values in externalValues.params and externalValues.data, i will need to create param3, param4 ... to get values from InternalValues, how i automaticaly get all values from externalValues.params and put them to execute the function? is it possible without send in array with one param ?

Thanks, i hope than u understand that exemple.

CodePudding user response:

Spread arguments will do it here:

// Values in DB
const InternalValues = {
  phone: '123456789',
  old: '25'
}

//Values from external inputs, when params old=param1 and phone=param2
const externalValues = {
  data: `return function exec(...params){
            console.log("values", ...params)
        };`,
  params: ['old', 'phone'],
}

// Create a function
const Func = new Function(externalValues.data)();

//Finding ExternalParams in Internal Values
const params = externalValues.params.map(p => InternalValues[p]);

// Execute Function
const ii = Func(...params);
  • Related