-2

Suppose I have a JS array like this:

[
  {
    "lat": 49.26125,
    "lon": -123.24807,
    "weight": 120
  },
  {
    "lat": 49.26125,
    "lon": -123.24807,
    "weight": 80
  },
  {
    "lat": 49.26125,
    "lon": -123.24807,
    "weight": 160
  },
  {
    "lat": 49.26229,
    "lon": 23.24342,
    "weight": 236
  },
  {
    "lat": 49.26229,
    "lon": 23.24342,
    "weight": 167
  }
]

Suppose I want to add up the weights the elements that have the same lat & lon to get something like this:

[
  {
    "lat": 49.26125,
    "lon": -123.24807,
    "weight": 360
  },
  {
    "lat": 49.26229,
    "lon": 23.24342,
    "weight": 403
  }
]

what's an efficient way to do that in JS?

  • 1
    Look at Array.prototype.reduce, or if you post what you've tried so far, we can assist you further. – fubar Mar 28 '17 at 05:58

3 Answers3

0

You could use a hash table as closure and a key with lat and lon as combined value.

Then check if the hash exists and if not generate a new object with the data and push it to the result set.

Later add weight to the property at the hashed object.

var data = [{ lat: 49.26125, lon: -123.24807, weight: 120 }, { lat: 49.26125, lon: -123.24807, weight: 80 }, { lat: 49.26125, lon: -123.24807, weight: 160 }, { lat: 49.26229, lon: 23.24342, weight: 236 }, { lat: 49.26229, lon: 23.24342, weight: 167 }],
    result = data.reduce(function (hash) {
        return function (r, a) {
            var key = ['lat', 'lon'].map(function (k) { return a[k]; }).join('|');
            if (!hash[key]) {
                hash[key] = { lat: a.lat, lon: a.lon, weight: 0 };
                r.push(hash[key]);
            }
            hash[key].weight += a.weight;
            return r;
        };
    }(Object.create(null)), []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can do this by reduce-ing your array to form a map from unique [lat, lon] pairs to a merged object that accumulates your total weight. Your result is then the list of values held by that map (which can be obtained using Object.keys and Array#map).

var array = [{lat:49.26125,lon:-123.24807,weight:120},{lat:49.26125,lon:-123.24807,weight:80},{lat:49.26125,lon:-123.24807,weight:160},{lat:49.26229,lon:23.24342,weight:236},{lat:49.26229,lon:23.24342,weight:167}]

var map = array.reduce(function (map, o) {
  var k = [o.lat, o.lon].join()
  
  if (k in map)
    map[k].weight += o.weight
  else 
    map[k] = o
  
  return map
}, {})

var result = Object.keys(map).map(function (k) { return map[k] })

console.log(result)
.as-console-wrapper { min-height: 100%; }
gyre
  • 16,369
  • 3
  • 37
  • 47
0

You can do something like this. It may not be that efficient but it works.

var arr = [{ lat: 49.26125, lon: -123.24807, weight: 120 }, { lat: 49.26125, lon: -123.24807, weight: 80 }, { lat: 49.26125, lon: -123.24807, weight: 160 }, { lat: 49.26229, lon: 23.24342, weight: 236 }, { lat: 49.26229, lon: 23.24342, weight: 167 }];

arr = arr.reduce(function(accumulation, currentElement){
    var samePosition = accumulation.find(function(obj){
        return obj.lat === currentElement.lat && obj.lng === currentElement.lng;
    });
    if(samePosition){
        samePosition.weight += currentElement.weight;
    }else{
        accumulation.push(currentElement);
    }
    return accumulation;
}, []);

console.log(arr);
Titus
  • 22,031
  • 1
  • 23
  • 33