Trying to get the maximum value of an array element using JavaScript tools:
On the server, the array is formed as follows:
$usr = mysql_query($query); while ($row = mysql_fetch_array($usr)) { $products_code_arr[] = $row[0]; } echo $products_code_arr; print_r indicates that we have an array of the form
Array ( [0] => 1237 [1] => 234 ) I take it as ajax and try to find the maximum value:
var url = 'get_data.php'; $.post( url, function(data) { var arr = data; function getMax(arr){ return Math.max.apply(Math,arr); } var max = getMax(arr); ); The method is described here , but it does not work for me, I get the error: Uncaught TypeError: CreateListFromArrayLike is called on non-object
Please suggest where the error is and how to fix it.
$.posthandler, add the lineconsole.log(data)and see what is displayed in the console. - Dmitry Shevchenko$.post()method should have an object with values of the form{ a: 'b', c: 'd' }as the second parameter, you have the same function (it should be the third one). - mixphparray tojs, what do you hope for? - mix