2017-09-26 41 views
0

所以我有一个任务,我正在努力,我不能为我的生活弄清楚如何完成最后一部分。分组整数数组

我有一个数组数组,我必须对这些数字进行排序,然后我必须将它们分组,每个数字有多少个。那是我陷入困境的一部分。

我做的这一切在一个文档中为这项任务:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet"> 
<title>Arrays For Days</title> 
<style> 
    body { 
     line-height: 1; 
     font-family: 'Lato', sans-serif; 
     letter-spacing: 0.08em; 
    } 
    p.headings { 
     font-style:italic; 
     font-size: 20px; 
    } 
</style> 
</head> 

<body> 
    <h1 style="text-decoration: underline;">Sorting and Grouping Arrays</h1> 
    <br> 
    <p class="headings">Starting List of Numbers:</p> 
    <p id="startNumbers"></p> 
    <br> 
    <p class="headings">Numbers Sorted:</p> 
    <p id="arraySorted"></p> 
    <br> 
    <p class="headings">Array Sorted and then Grouped:</p> 
    <p id="arrayGrouped"></p> 

    <script> 

     var numbers = [1, 5, 17, 4, 9, 3, 1, 17, 32, 5, 3, 27, 9, 18, 3, 12, 67, 18, 32, 1, 19, 21, 1, 17]; 
      //printed original numbers to HTML element using document.getElementById 
     document.getElementById("startNumbers").innerHTML = numbers; 
      //used .sort method, with a comparative function using a-b, so if a is less than b then the result... 
      //should be a sorting of lesser to greater. 
     numbers.sort (
      function(a, b) { 
       return a - b; 
     }); 
      //then I printed the now sorted numbers to matching HTML element using document.getElementById 
     document.getElementById("arraySorted").innerHTML = numbers; 




     document.getElementById("arrayGrouped").innerHTML = output; 

    </script> 
</body> 
</html> 

只是试图让数字来显示我的网页是这样的:

1:4,3:3, 4:1,5:2,9:2,12:1,17:3,18:2,19:1,21:1,27:1,32:2,67:1

这是可能的但我一直只找到不像这样的例子,并且不得不处理一些其他与数组相关的东西,我不需要这个任务。谢谢你的人的帮助:-)

弥敦道L.

+0

如果你能找到一种方法来排序,而无需使用数组,你会赢得所有的代码。 –

+0

是的,不幸的是我必须这样做:-( –

回答

0

您可以使用.map().filter()摆脱原来的阵列过滤当前元素的.length。从结果数组中删除重复项的一个选项是将数组传递给Set的构造函数,将Set转换回Array.from()的数组。

document.getElementById("arraySorted") 
.innerHTML = Array.from(new Set(numbers.map(n => `${n}:${numbers.filter(_n => _n === n).length}`))); 
+0

几乎它似乎把每个数字1四次1:4,看起来像这样:1:4,1:4,1:4,1:4 ,3:3,3:3,3:3,4:1,5:2,5:2,9:2,9:2,12:1,17:3,17:3,17:3,18 :2,18:2,19:1,21:1,27:1,32:2,32:2,67:1 –

+0

@NathanL查看最新文章 – guest271314

+0

嗯,它的工作原理,但我想我需要找到更多比较老的JS排序和分组方式,而不是1行固定器,我喜欢你所做的,但它的效果非常好,但我试图保持相对简单和基本。 –

0

我的解决方案是只使用Array.prototype.reduce函数。要对这些数字进行分组,您需要将元素添加到新数组中,并将每个数字与新数组的最后一项进行比较。如果相同,只需在最后一项的累加器中加1,否则为新数字创建一个新元素。我希望下面的代码使得它有点清楚:

var groupedNumders = numbers.reduce(function(ac, currentValue) { // ac for accumulator 
    if (currentValue === ac[ac.length - 1][0]) 
     ac[ac.length - 1][1] += 1; 
    else 
     ac.push([currentValue, 1]) 
    return ac 
    }, [[numbers[0], 0]]) 

如果您是通过什么Array.prototype.reduce呢,是指MDN混淆。

要创建一个打印字符串,你可能会再次降低阵列:

var output = groupedNumbers.reduce(function(s, a) {return s + a[0] + ":" + a[1] + ", "}, "")