Q: How can I change this PHP array function to JS code? Functiont_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, 14, 7.1, 3.1, 4.25, 1.83, 1.10, 1.766, 3 Q: How can I modify this PHP array function to JS code?
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, 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, 6.4, 7.7, 4.083, 1.70, 3.95, 10, 3.12, 5.88, 4.6, 5.88, 4.7, 7.5 );
$ Count = 0;
Foreach ($ arr as $ key => $ value ){
$ Count + = ($ ActName [$ key] * $ value );
}
}
The variable $ arr imports an array, such as, 32, 3, and 14, corresponding to $ ActName, which determines the input array and obtains the sum value based on the serial number, how to change to JS.
------ Solution --------------------
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); }