簡體   English   中英

搜索並計算特定命令在數組中出現了多少次

[英]Search and count how many times do the specific command appears in array

我搜索后什么也沒找到。 這是我的測試用例:

console.log(specificSearch([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ], 'even')); // the maximum number of even numbers is in row - 2, which are 2 and 8

  console.log(specificSearch([
  ['o', 'o', 'o', 'x'],
  ['x', 'x', 'o'],
  ['o', 'x'],
  ['x', 'x', 'x', 'x', 'x', 'x', 'x']
  ], 'x')); // the maximum number of x is in column - 4, which is 7 times appear

這是我到目前為止的代碼:

function specificSearch(array, command) {

    var max = 0
    var even = 0
    for(var i = 0; i < array.length; i++){
      var evenCounter = 0
       for(var j = 0; j < array[i].length; j++){
          if(command === 'even'){
             if(array[i][j] % 2 == 0){
                evenCounter++
             }
          } 
       }

       if(command === 'even' ){
         if( max < evenCounter) {
           max = evenCounter
           even = i
         }
       }
    }
    return even
  }

那就是當我嘗試搜索偶數時,如果要搜索偶數,則必須返回行號和什么數字,但另一方面,如果不搜索數字並且數組的長度不相同,則需要在哪一列中返回,有多少次出現 這種情況不需要內置函數(例如正則表達式,映射,過濾器,索引),只需使用循環和數組操作(例如推,彈出,移位等)即可

謝謝您的幫助,我只是一個嘗試學習代碼的菜鳥:)

這種情況下不需要內置功能

通常,當您要將數組轉換為一個值時,可以使用Array.prototype.reduce

 const specificSearch = (array, comparer) => array.reduce((highest, items) => { //reduce to highest number const current = items.reduce( //reduce to number //if comparer returns true for item add 1 to sum (sum, item) => (comparer(item) ? sum + 1 : sum), 0,//start with sum of 0 ); //if current result is higher than highest so far // return current, else return highest return current > highest ? current : highest; }, 0/** start with highest value of 0 */); console.log( "max times even in item array", specificSearch( [[1, 2, 3], [4, 5, 6], [7, 8, 9]], (x) => x % 2 === 0, ), ); console.log( "max times value of 'x' in item array", specificSearch( [ ['o', 'o', 'o', 'x'], ['x', 'x', 'o'], ['o', 'x'], ['x', 'x', 'x', 'x', 'x', 'x', 'x'], ], (x) => x === 'x', ), ); 

如果您看到代碼片段,那么您會發現可以將數組轉換為帶有內部reduce的數字,而外部reduce則選擇最高的數字。

內部reduce使用了specificSearch調用者傳遞的比較器函數來指示總和何時需要增加,現在調用者可以確定如何使用specificSearch

您可以移交一個用於檢查特定值的函數,然后為檢查返回一個布爾值。

  • even

     x => !(x % 2)` 
  • 進行身份檢查

     x => x === 'x' 

然后,你可以收集所有計數的行和列陣列,並獲得更高的最大值,並從返回rows / cols數組的索引與最大值。

結果是對象的行/列的最大計數以及發生最大計數的索引。

順便說一句,這個答案使用索引,因為它們在Javascript中起作用,從零開始。 如果需要以1開頭,則只需為每個索引添加1。

 function specificSearch(array, checkFn) { function getIndices(array, value) { var i, indices = []; for (i = 0; i < array.length; i++) { if (array[i] === value) indices.push(i); } return indices; } var i, j, rows = [], cols = [], max; for (i = 0; i < array.length; i++) { for (j = 0; j < array[i].length; j++) { if (checkFn(array[i][j])) { rows[i] = (rows[i] || 0) + 1; cols[j] = (cols[j] || 0) + 1; } } } max = Math.max(...cols, ...rows); return { max, rows: getIndices(rows, max), cols: getIndices(cols, max) }; } console.log(specificSearch([[1, 2, 3], [4, 5, 6], [7, 8, 9]], x => !(x % 2))); console.log(specificSearch([['o', 'o', 'o', 'x'], ['x', 'x', 'o'], ['o', 'x'], ['x', 'x', 'x', 'x', 'x', 'x', 'x']], x => x === 'x')); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM