将字符串中的变量值替换为函数调用

时间:2016-05-26 16:18:28

标签: javascript

我是新手,如果我的术语没有意义,请原谅我。这就是......

如果我在JS中有以下变量:

var LE_TotalAnnualIncome = "50000";
var LE_TypeOfSP = "Single";
var LE_NoDependants = "1";
var LE_Customer = "3000";

如何转换字符串:

MinimumLivingExpenses(LE_TotalAnnualIncome,LE_TypeOfSP,LE_NoDependants,LE_Customer)

要:

MinimumLivingExpenses("50000","Single","1","3000")

编辑添加: 这段代码可能解释了我想要实现的目标:

function Calculate()
{
var elements = frm_livingexpenses.elements;
var el;
var reClass = /(^|\s)inputCalculated(\s|$)/;

// Searches for fields that need to be calculated
for (var i=0, iLen=elements.length; i<iLen; i++) 
{
    el = elements[i];
    if (reClass.test(el.className))
    {
        // if contents of element are a formula, calculate result
        // the element will have an '=' if it's a formula
        var GetFormula = document.getElementById(elements[i].name).getAttribute('data-formula');
        if (GetFormula.substring(0,1) == '=')
        {
            elements[i].value = eval(GetFormula.substring(1,999));
            eval(elements[i].name.substring(12,999) + '="' + elements[i].value + '";');
            // this is where the variables are set. eg
            // var LE_TotalAnnualIncome = "50000";
            // var LE_TypeOfSP = "Single";
            // var LE_NoDependants = "1";
            // var LE_Customer = "3000";
        }

        // if contents of element are a function call, send to function
        // the element will have a '#' at the front if I need to call a function
        if (GetFormula.substring(0,1) == '#')
        {
        // eg. #MinimumLivingExpenses(LE_TotalAnnualIncome,LE_TypeOfSP,LE_NoDependants,LE_Custo‌​mer)
        // this should be:  MinimumLivingExpenses("50000","Single","1","3000")
        alert('Ajax call will use this in URL='+GetFormula.substring(1,999));
        }
    }
}

}`

2 个答案:

答案 0 :(得分:0)

实现目标的一种方法是创建一个存储变量的对象。请注意添加的benefitcreatedAt任何内容。为了做到这一点:

  • 定义对象:

    ref.child('as/:aId/bs')
      .orderByChild('status')
      .equalTo('OPEN')  
      .once('value')
      .then(filterBsThatDontFulFillCreatedAtCondition)
      .then(snapshot => snapshot.forEach(...) // Access /bs/:bId where :bId = snapshotItem.key() 
    
  • 使用bracket notation将变量添加为属性 - 只需将eval更改为此:

    var reClass = /(^|\s)inputCalculated(\s|$)/;
    var variables = {};
    
  • 通过从对象获取值来构造字符串。请注意,我创建了一个新的变量列表,而不是简单地遍历对象的所有属性。原因是循环对象属性并不能保证任何类型的订单。

    首先构建列表并将其保存到变量(选择单向):

    eval()

    最后,开始构建最终字符串:

    // Empty string concatenation to make sure the value stored is cast to string
    variables[elements[i].name.substring(12,999)] = '' + elements[i].value;
    

<强>更新 我不知道,前12个字符中的名字是什么,你只能获得下一个999个字符,这就是我保持逻辑不变的原因。但是你可以使用name.slice(12)从第12个索引到字符串的结尾。

答案 1 :(得分:0)

如果您真的想要在字符串中进行替换,那么您可以使用replace

&#13;
&#13;
var LE_TotalAnnualIncome = "50000";
var LE_TypeOfSP = "Single";
var LE_NoDependants = "1";
var LE_Customer = "3000";
    
var str = "MinimumLivingExpenses(LE_TotalAnnualIncome,LE_TypeOfSP,LE_NoDependants,LE_Customer)";
str = str.replace("LE_TotalAnnualIncome", LE_TotalAnnualIncome)
         .replace("LE_TypeOfSP", LE_TypeOfSP)
         .replace("LE_NoDependants", LE_NoDependants)
         .replace("LE_Customer", LE_Customer);
console.log(str);
&#13;
&#13;
&#13;

当使用字符串作为第一个参数调用时,它将用第二个参数替换该字符串的第一个次出现。

相关问题