日期:2014-05-17  浏览次数:20393 次

求问:这段PHP数组函数如何修改成JS代码?
function t_arr($weight,$arr)
{

$ActName=array(1.464,3.9,3.7,3.7,2.28,1.2,1.466,2.0,1.90,2.80,0.8,2.166,14,7.1,3.1,4.25,1.83,1.10,1.766,3,1.38,6,9.3,14,9.3,12,5.0,4.38,5.8,12.466,13.166,4.2,5,5,6.4,7.7,4.083,1.70,3.95,10,3.12,5.88,17,4,4.6,5.88,9,9,4.7,20,10,15,25,7.5);

$count=0;

foreach ($arr as $key => $value) {

$count+=($ActName[$key]*$value);

}

}

变量$arr传入的是数组,如21,32,3,14这类的,对应于$ActName的,判断传入的数组有哪些,然后根据序号得到相加的值,如何修改为JS的。


------解决方案--------------------
JScript code
function t_arr(weight,arr)
{

    ActName=[1.464,3.9,3.7,3.7,2.28,1.2,1.466,2.0,1.90,2.80,0.8,2.166,14,7.1,3.1,4.25,1.83,1.10,1.766,3,1.38,6,9.3,14,9.3,12,5.0,4.38,5.8,12.466,13.166,4.2,5,5,6.4,7.7,4.083,1.70,3.95,10,3.12,5.88,17,4,4.6,5.88,9,9,4.7,20,10,15,25,7.5];

    count=0;
    if(arr instanceof Array){
        for(key in arr) {

             count+=(ActName[key]*arr[key]);

        }
    }
   alert(count);    

}