2016-12-07 57 views
4

我有一組數字,顯示如下;如何將數組中的每個數字相互比較? (javascript)

var data = "615:415,600:400,600:400,300:300"

每個數字表示的x/y座標,並且我想旁邊添加其基於數量的範圍內的頻率計算出的各一個的值。

因此,我希望能夠比較每個值與該字符串中的所有其他值,並從中執行以下功能;

  1. 從字符串中刪除的數目,如果它是一個重複,並添加:1
  2. 如果X/Y的數字針對任何其他號碼的範圍的15內的是兩個,添加:1
  3. 如果沒有匹配,地址:0
  4. 轉成數組

因此,使用數據線,將它轉化爲;

var data = "615:415:1, 600:400:2, 300:300:0"

我一直在嘗試這種使用減速功能做的,但我與主要步驟2.我希望有人可以幫忙掙扎?

Thanks - Code + Plunk below!


http://plnkr.co/edit/zPW1844cLnUFAlEI77jq?p=preview

var result = []; 
 
var data = "615:415,600:400,600:400,300:300" 
 
var count = 0; 
 

 
var reducer = function(p, c, i, a) { 
 

 
    if (p && p !== c) { 
 

 
    var _t = p.split(":"); 
 
    result.push({ 
 
     x: _t[0], 
 
     y: _t[1], 
 
     value: count 
 
    }); 
 

 
    count = 0; 
 
    if (i === a.length - 1) { 
 
     _t = c.split(":"); 
 
     result.push({ 
 
     x: _t[0], 
 
     y: _t[1], 
 
     value: count 
 
     }); 
 
    } 
 
    } 
 
    else { 
 
    count++; 
 
    } 
 
    return c 
 
} 
 

 
data.split(',').sort().reduce(reducer); 
 

 
console.log(result)

+0

_ 「如果X/Y號針對任何其他號碼的範圍內15均,加:1」 _你爲什麼要加上'0'在'300:300: 0'?爲什麼'2'連接在'600:400:2'? – guest271314

+0

@ guest271314這個想法是有一個跑步計數器,所以如果一個數字沒有重複/不在任何其他數字的範圍內,它應該被設置爲0,因此可以將其與其他數字進行比較。它是'600:400:2',因爲它在'615:415'的範圍內,所以它獲得+1,但也因爲它是重複的,所以它獲得了另一個+1(重複刪除) – ggt

回答

1

你可以用一步一步的方法,並在座標第一分割字符串,生成用於計數和過濾座標的哈希表只有唯一的座標。

然後比較每個唯一的座標,並計數是否在給定的範圍內。

稍後用count計算座標並加入字符串。

var data = "615:415,600:400,600:400,300:300", 
 
    result = function (array) { 
 
     var i, j, 
 
      hash = Object.create(null), 
 
      unique = array.split(',').filter(function (a) { 
 
       var parts = a.split(':'); 
 
       if (!hash[a]) { 
 
        hash[a] = [parts[0], parts[1], 0]; // [x, y, count] 
 
        return true; 
 
       } 
 
       hash[a][2]++; 
 
      }); 
 

 
     for (i = 0; i < unique.length - 1; i++) { 
 
      for (j = i + 1; j < unique.length; j++) { 
 
       if (
 
        Math.abs(hash[unique[i]][0] - hash[unique[j]][0]) <= 15 && 
 
        Math.abs(hash[unique[i]][1] - hash[unique[j]][1]) <= 15 
 
       ) { 
 
        hash[unique[i]][2]++; 
 
        hash[unique[j]][2]++; 
 
       } 
 
      } 
 
     } 
 
     return unique.map(function (a) { 
 
      return hash[a].join(':'); 
 
     }).join(', '); 
 
    }(data); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+1

完美 - 正是我所期待的謝謝! – ggt

1

這裏有一個選擇:

var data = "615:415,600:400,600:400,300:300"; 
 

 
var result = (function (s) { 
 
    var result = {}; 
 
    var values = []; 
 
    // Process each value 
 
    s.split(',').forEach(function (v) { 
 
    var b = v.split(':'); 
 
    // If a match, increment count by 2 (once for match and again for within 15) 
 
    if (result[v]) { 
 
     result[v].count += 2; 
 

 
    // Otherwise, just check for within 15 
 
    } else { 
 
     result[v] = {x:b[0], y:b[1], count:0}; 
 
     values.forEach(function(xy, i){ 
 
     if (xy[0]>= (b[0]-15) && xy[0] <= (+b[0]+15) && 
 
      xy[1]>= (b[1]-15) && xy[1] <= (+b[1]+15)) { 
 
      ++result[xy.join(':')].count; // Increment for nearby only 
 
     } 
 
     }) 
 
     values.push([b[0],b[1]]); 
 
    } 
 
    }) 
 
    // Create required string format 
 
    return Object.keys(result).reduce(function(arr, key){ 
 
    arr.push(key + ':' + result[key].count); 
 
    return arr; 
 
    },[]).join(', '); 
 
})(data) 
 

 
console.log(result);

0

所有的答案至今都不錯。我只想通過發明一種Array.prototype.withEachOther()方法來引入一點點變化。這隻需要一個回調就可以調用與數組中的每個其他項目的回調,因爲它可能是建議的參數。它的工作原理。

Array.prototype.withEachOther = function(cb){ 
 
            this.map(function(e,i,a){ 
 
              var t = a.slice(); 
 
              t.splice(0,i+1); 
 
              t.map(function(f){ 
 
                a[i] = cb(e,f); 
 
                }); 
 
              }); 
 
            return this; 
 
           }; 
 

 
var data = "615:415,600:400,600:400,300:300, 550 : 550".split(/\s*,\s*/) 
 
                 .map(s => s.split(/\s*:\s*/).concat(0)), 
 
     cb = (f,s) => (Math.abs(f[0]-s[0]) <= 15 && Math.abs(f[1]-s[1]) <= 15 && (f[2]++, s[2]++),f); 
 
    result = data.reduceRight(function(p,c,i,a){ 
 
           var fi = a.slice(0,i-a.length) 
 
             .findIndex(f => f[0] === c[0] && f[1] === c[1]); 
 
           fi !== -1 ? (a[fi][2] += ++c[2], a.splice(i,1)) 
 
             : p.push(c); 
 
           return p; 
 
          },[]) 
 
       .withEachOther(cb) 
 
       .reduce((p,c) => p += c[0]+":"+c[1]+":"+c[2]+", ",""); 
 
console.log(result);