How to compare each number in an array with each other? (Javascript)

I have a set of numbers that display as follows:

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

Each number represents the x / y coordinate, and I would like to add a value next to each, which is calculated based on the frequency of the number within the range.

So, I would like to be able to compare each value with everyone else in this line, and from this the following functions are executed:

  • Remove the number from the line if it is a duplicate and add: 1
  • If x / y numbers range from 15 to any other number, add: 1
  • If there are no matches, add: 0
  • Include array

Thus, using the data string, it will be converted to:

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

I try to do this using the reducer function, but I'm mostly afraid in step 2. I hope someone can help?

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)
Hide result
+4
3

, - .

, .

.

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; }
Hide result
+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);
Hide result
+1

. , 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);
Hide result
0

Source: https://habr.com/ru/post/1663064/


All Articles