Javascript对象数组变成字符串?

时间:2013-09-05 19:17:11

标签: javascript jquery arrays object

jsFiddle - > http://jsfiddle.net/rVLN2/1/

我是菜鸟 - 我知道我的脚本效率低下,但我正在研究它。我有一个对象数组设置,如:

var SatWaterMetric = [
  {"T":0,"Psat":0.6117,"vf":0.001,"vg":206,"hf":0.001,"hfg":2500.9,"hg":2500.9},
  {"T":5,"Psat":0.8725,"vf":0.001,"vg":147.03,"hf":21.02,"hfg":2489.1,"hg":2510.1},
  {"T":10,"Psat":1.2281,"vf":0.001,"vg":106.32,"hf":42.022,"hfg":2477.2,"hg":2519.2},
  ...................................];

然后我有一个从html中提取值的函数,并根据表格插入数值。

 function interpolate(myval, unit) {

    if (unit == "T") {
        for (var i=0;i<SatWaterMetric.length;i++) {
            if (myval < SatWaterMetric[i].T) {

                T_low = SatWaterMetric[i-2].T;
                T_high = SatWaterMetric[i-1].T;

                Psat_low = SatWaterMetric[i-2].Psat;
                Psat_high = SatWaterMetric[i-1].Psat;                    

                vf_low = SatWaterMetric[i-2].vf;
                vf_high = SatWaterMetric[i-1].vf;

                vg_low = SatWaterMetric[i-2].vg;
                vg_high = SatWaterMetric[i-1].vg;

                hf_low = SatWaterMetric[i-2].hf;
                hf_high = SatWaterMetric[i-1].hf;

                hfg_low = SatWaterMetric[i-2].hfg;
                hfg_high = SatWaterMetric[i-1].hfg;

                hg_low = SatWaterMetric[i-2].hg;
                hg_high = SatWaterMetric[i-1].hg;

                var factor = (myval - T_low) / (T_high - T_low);

                Psatx = (1 * Psat_low) + ( 1 * factor * (Psat_high - Psat_low));
                vfx = (1 * vf_low) + ( 1 * factor * (vf_high - vf_low)); 
                vgx = (1 * vg_low) + ( 1 * factor * (vg_high - vg_low)); 
                hfx = (1 * hf_low) + ( 1 * factor * (hf_high - hf_low)); 
                hfgx = (1 * hfg_low) + ( 1 * factor * (hfg_high - hfg_low)); 
                hgx = (1 * hg_low) + ( 1 * factor * (hg_high - hg_low));
                Tx = myval;
                break;
            }
        }

        $('#txtpsat').val(Math.round(Psatx*100000)/100000);
        $('#txtvf').val(Math.round(vfx*10000000)/10000000);
        $('#txtvg').val(Math.round(vgx*100000)/100000);
        $('#txthf').val(Math.round(hfx*10000)/10000);
        $('#txthg').val(Math.round(hgx*100)/100);
        $('#txthfg').val(Math.round(hfgx*100)/100);

    } else if (unit == "Psat") {

        //Repeat everything but for Psat

    } else if (unit == "vf") {

        //Repeat everything but for vf

               } else {}
}

});

现在这只是为了解决“T”已知的值。我正在尝试创建一个程序来确定输入哪个值(“Psat”,“vg”,“vf”等等),然后根据该值评估所有其他数字。我现在有这种设置的方式,我基本上必须重复Psat,vg,vf,hg,hf和hfg的所有代码。这似乎非常低效。有没有办法将其减少到一个平滑的功能?

。 jsFiddle - &gt; http://jsfiddle.net/rVLN2/1/

3 个答案:

答案 0 :(得分:4)

你错过了很酷的javascript功能。

obj.Prop

相同
obj['Prop']

所以,你要做的就是使用unit作为值的关键。

答案 1 :(得分:1)

我被打败了!好吧,无论如何,这是实施......

SatWaterMetric[i].T更改为SatWaterMetric[unit]

jsFiddle

function interpolate(myval, unit) {
            for (var i=0;i<SatWaterMetric.length;i++) {
                if (myval < SatWaterMetric[i][unit]) {

                    T_low = SatWaterMetric[i-2].T;
                    T_high = SatWaterMetric[i-1].T;

                    Psat_low = SatWaterMetric[i-2].Psat;
                    Psat_high = SatWaterMetric[i-1].Psat;                    

                    vf_low = SatWaterMetric[i-2].vf;
                    vf_high = SatWaterMetric[i-1].vf;

                    vg_low = SatWaterMetric[i-2].vg;
                    vg_high = SatWaterMetric[i-1].vg;

                    hf_low = SatWaterMetric[i-2].hf;
                    hf_high = SatWaterMetric[i-1].hf;

                    hfg_low = SatWaterMetric[i-2].hfg;
                    hfg_high = SatWaterMetric[i-1].hfg;

                    hg_low = SatWaterMetric[i-2].hg;
                    hg_high = SatWaterMetric[i-1].hg;

                    var factor = (myval - T_low) / (T_high - T_low);

                    Psatx = (1 * Psat_low) + ( 1 * factor * (Psat_high - Psat_low));
                    vfx = (1 * vf_low) + ( 1 * factor * (vf_high - vf_low)); 
                    vgx = (1 * vg_low) + ( 1 * factor * (vg_high - vg_low)); 
                    hfx = (1 * hf_low) + ( 1 * factor * (hf_high - hf_low)); 
                    hfgx = (1 * hfg_low) + ( 1 * factor * (hfg_high - hfg_low)); 
                    hgx = (1 * hg_low) + ( 1 * factor * (hg_high - hg_low));
                    Tx = myval;
                    break;
                }
            }

            $('#txtpsat').val(Math.round(Psatx*100000)/100000);
            $('#txtvf').val(Math.round(vfx*10000000)/10000000);
            $('#txtvg').val(Math.round(vgx*100000)/100000);
            $('#txthf').val(Math.round(hfx*10000)/10000);
            $('#txthg').val(Math.round(hgx*100)/100);
            $('#txthfg').val(Math.round(hfgx*100)/100);
    }

答案 2 :(得分:1)

如前所述,您可以利用引用对象属性的事实可以使用点和数组表示法完成。

更进一步,您不仅可以简化对传入unit的访问,还可以进一步减少代码的其余部分。

首先,通过稍微更改标记,所有输入字段ID将具有相同的模式“txt”+ key.toLowerCase():

<input type='text' size='8' id='txtt'/>

使用预定义的幅度初始化highLow值的对象(T没有):

var highLow = {
    T:    {},
    Psat: {magnitude: 100000},
    vf:   {magnitude: 10000000},
    vg:   {magnitude: 100000},
    hf:   {magnitude: 10000},
    hg:   {magnitude: 100},
    hfg:  {magnitude: 100}
};

然后可以修改插值函数的其余部分,使其看起来像这样:

// Note 1: Started loop at 2 to avoid undefined array values
// Note 2: the redundant loops could be reduced by calculating T first
function interpolate(myval, unit) {
    var key, cur, factor;

    var highLow = {/*see above*/};

    for (var i = 2; i < SatWaterMetric.length; i++) {
        if (myval < SatWaterMetric[i][unit]) {
            for (key in highLow) {
                highLow[key].low = SatWaterMetric[i - 2][key];
                highLow[key].high = SatWaterMetric[i - 1][key];
            }

            factor = (myval - highLow.T.low) / (highLow.T.high - highLow.T.low);

            for (key in highLow) {
                cur = highLow[key];

                // This is T
                if (!cur.magnitude) {
                    cur.result = myval;
                } else {
                    cur.result = (1 * cur.low) + (1 * factor * (cur.high - cur.low));
                }
            }
            break;
        }
    }

    for (key in highLow) {
        cur = highLow[key];
        $('#txt' + key.toLowerCase()).val(Math.round(cur.result * (cur.magnitude || 1)) / (cur.magnitude || 1));
    }
}

Demo jsFiddle


版本2:此版本通过重构显示计算并将其直接分配给关联的highLow键并计算内联的最终显示值,从前一示例中删除冗余循环。

Demo jsFiddle

代码:

// Generates a result for display using the provided magnitude to 
// format the calculated value.
function calcFn(T, factor, low, high, magnitude) {
    var retVal = (1 * low) + (1 * factor * (high - low));

    return Math.round(retVal * (magnitude || 1)) / (magnitude || 1);
}

var highLow = {
    // Initializing T with a calc function that just returns the passed in value
    // This makes the calc/display loop much simpler
    T:    { calc:   function(T) { return T; }, 
            factor: function(T, low, high) { return (T - low) / (high - low); } },

    // All other metrics will use calcFn
    Psat: { magnitude:   100000, calc: calcFn },
    vf:   { magnitude: 10000000, calc: calcFn },
    vg:   { magnitude:   100000, calc: calcFn },
    hf:   { magnitude:    10000, calc: calcFn },
    hg:   { magnitude:      100, calc: calcFn },
    hfg:  { magnitude:      100, calc: calcFn }
};

// Note: Started loop at 2 to avoid undefined array values
function interpolate(myval, unit) {
    var key, cur, factor, result, high, low;

    for (var i = 2; i < SatWaterMetric.length; i++) {
        if (myval < SatWaterMetric[i][unit]) {
            // T is in slot 0
            factor = highLow.T.factor(myval, SatWaterMetric[i - 2].T, SatWaterMetric[i - 1].T);

            // Now we can simply loop through all metrics using the assigned calc function to 
            // generate a result for display.
            for(key in highLow) {
                cur    = highLow[key];
                low    = SatWaterMetric[i - 2][key];
                high   = SatWaterMetric[i - 1][key];
                result = cur.calc(myval, factor, low, high, cur.magnitude);

                $('#txt' + key.toLowerCase()).val(result);
            }

            break;
        }
    }
}