简体   繁体   English

有没有更简单的方法来计算数组中行中的相同项目?

[英]Is there more easier way to count same items that goes in row in array?

This is my code这是我的代码

function repeat(arr){
    let string = (arr.join(''))
    let start = -1
    let count = 0
    while((start = string.indexOf('hhh', start + 1)) != -1 ){
        count += 1
        start += 3
    }
    while((start = string.indexOf('ttt', start + 1)) != -1 ){
        count += 1
        start += 3
    }
    console.log(count)
}

repeat(["h", "h", "h", "t", "h", "h", "t", "t", "t", "h", "t", "h", "h", "h", "h"])

In this example I tried to count how many times t or h goes 3 times in row.在此示例中,我尝试计算 t 或 h 连续出现 3 次的次数。

Is there a way to do it without making a string?有没有办法在不制作字符串的情况下做到这一点? Or at least to merge block with while into one?或者至少将块与 while 合并为一个?

You can iterate each element in the array.您可以迭代数组中的每个元素。

While doing so, you can keep track of:这样做时,您可以跟踪:

  • the total number of repeated groups that have occurred ( total )已出现的重复组总数( total
  • the value of the previous element seen in the array ( previousElement )数组中前一个元素的值 ( previousElement )
  • the current number of elements that have repeated ( currentCount )当前重复的元素数 ( currentCount )

Below is an example of what I described above, including comments:以下是我上面描述的示例,包括注释:

 function countRepeats (numOfRepeated, array) { let total = 0; let currentCount = 0; // Start with an object because no object strictly equals any other value: let previousElement = {}; for (const element of array) { if (element === previousElement) { // Increment the count: currentCount += 1; if (currentCount === numOfRepeated) { // Increment the total: total += 1; // Reset the count to 1: currentCount = 1; } } else { // Reset the count to 1: currentCount = 1; } // Update the previous element: previousElement = element; } return total; } const total = countRepeats( 3, ["h", "h", "h", "t", "h", "h", "t", "t", "t", "h", "t", "h", "h", "h", "h"], // 1 2 3 1 1 2 1 2 3 1 1 1 2 3 1 // 1 2 3 ); console.log(total); // 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为更多 ajax 项目制作 if (test &gt;= 0 &amp;&amp; test &lt;=3) 的更简单方法 - Easier way to make if (test >= 0 && test <=3) for more ajax items 将其阵列简化为 map - Reduce it array to an easier way to map 从数组中获取价值的更简单方法 - easier way to get value from array 如何用数组中的特殊形式计数相同项目(javascript) - How to count same items with specail form in a array (javascript) 有没有一种有效的方法来组织由两个或多个项目组成的多维数组? - Is there an efficient way to organize a multi-dimension array by two or more items? 是否有更简单/更有效的方法来更新此 json 文件中的值? - Is there an easier/more efficient way to update values in this json file? 将数组中的相同元素组合为数组中的一个字符串的更简洁的方法? - More condense way to combine same elements in array to one string in array? 如何计算 Java 脚本数组中的项目,但仅当项目彼此相邻时? - How can I count items in Java Script array but only when items are the same next to each other? 当集合中的项目数超过三个时覆盖记录 - Overwriting records when number of items in collection goes more than three 如果文本超过 2 个字符,则获取上一页计数 - Get previous page count if text goes more than 2 characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM