Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在一个数组中输出两个以上值的最大值_Javascript - Fatal编程技术网

Javascript在一个数组中输出两个以上值的最大值

Javascript在一个数组中输出两个以上值的最大值,javascript,Javascript,我有一段代码,它输出document.write中的最高值,使用我在该行中声明的数组。这工作正常,输出56,这是最高的 function topProduct(productProfitArray) { if (toString.call(productProfitArray) !== "[object Array]") return false; return Math.max.apply(null, productProfitArray); }

我有一段代码,它输出document.write中的最高值,使用我在该行中声明的数组。这工作正常,输出56,这是最高的

  function topProduct(productProfitArray) {
     if (toString.call(productProfitArray) !== "[object Array]")  
       return false;
  return Math.max.apply(null, productProfitArray);
    }

document.write(topProduct([12,34,56,1]));
现在,我想在声明的数组上输出值,同时在int值旁边输出一个字符串,但是我得到了
Uncaught SyntaxError:Invalid或unexpected token

var productProfitArray = [ {“Product A”: -75}, {“Product B”: -70}, {“Product C”:
98}, {“Product D”: 5}, {“Product E”: -88}, {”Product F”: 29}];


 function topProduct(productProfitArray) {
     if (toString.call(productProfitArray) !== "[object Array]")  
       return false;
  return Math.max.apply(null, productProfitArray);
    }

document.write(topProduct(productProfitArray));

我尝试添加一个变量productProfitArray,然后执行该函数,然后尝试使用document.write输出该变量。任何帮助都将不胜感激。

您应该映射数组以获取值:

var productProfitArray=[{“产品A”:-75},{“产品B”:-70},{“产品C”:98},{“产品D”:5},{“产品E”:-88},{“产品F”:29}];
函数topProduct(productProfitArray){
if(toString.call(productProfitArray)!=“[object Array]”)
返回false;
返回Math.max.apply(null,productProfitArray.map(o=>Object.values(o)[0]);
}

编写(topProduct(productProfitArray))更改对象键中的引号