Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.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_Arrays_Reactjs - Fatal编程技术网

Javascript 在数组中排列数字,以便创建最大的数字

Javascript 在数组中排列数字,以便创建最大的数字,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,给定一个数字数组,以产生最大值的方式排列它们。例如,如果给定的数字是{54,546,548,60},则排列6054854654给出最大值 var maxCombine = (a) => +(a.sort((x, y) => +("" + y + x) - +("" + x + y)).join('')); var b = [54, 546, 548, 60]; // test & output console.log([ b ].map(maxCombine)); 它工

给定一个数字数组,以产生最大值的方式排列它们。例如,如果给定的数字是{54,546,548,60},则排列6054854654给出最大值

var maxCombine = (a) => +(a.sort((x, y) => +("" + y + x) - +("" + x + y)).join(''));
var b = [54, 546, 548, 60];
// test & output
console.log([
  b
].map(maxCombine));
它工作正常,并在日志上打印答案。但当我在视图中使用它时,它不会呈现任何内容

return (
  <div>{[b].map(maxCombine))}</div>
) 
返回(
{[b].map(maxCombine))}
) 

它应该可以工作,在您的情况下,输出将是数组中的单个元素

试试这个:

return (
   <div>{([b].map(maxCombine))[0]}</div>
) 

它返回带有[]方括号的值,我使用parseInt()删除它们,可以吗?@HUSSAIN,我想你可以直接调用
maxCombine(b)
。它将返回正确的结果。
return (
  <div>{JSON.stringify([b].map(maxCombine))}</div>
)