Javascript 数组组合元素以获得唯一数组
Javascript Array combine elements to get a unique array
假设我有一个这样的 JS 数组:
[
{
"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
}
]
假设我想将具有相同 lat 和 lon[= 的元素相加 weights 22=] 得到这样的东西:
[
{
"lat": 49.26125,
"lon": -123.24807,
"weight": 360
},
{
"lat": 49.26229,
"lon": 23.24342,
"weight": 403
}
]
在 JS 中执行此操作的有效方法是什么?
您可以使用散列 table 作为闭包,并使用包含 lat
和 lon
的键作为组合值。
然后检查散列是否存在,如果不存在则用数据生成一个新对象并将其推送到结果集。
稍后将 weight
添加到散列对象的 属性。
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; }
您可以通过 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%; }
你可以这样做。它可能没有那么有效,但它确实有效。
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);
假设我有一个这样的 JS 数组:
[
{
"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
}
]
假设我想将具有相同 lat 和 lon[= 的元素相加 weights 22=] 得到这样的东西:
[
{
"lat": 49.26125,
"lon": -123.24807,
"weight": 360
},
{
"lat": 49.26229,
"lon": 23.24342,
"weight": 403
}
]
在 JS 中执行此操作的有效方法是什么?
您可以使用散列 table 作为闭包,并使用包含 lat
和 lon
的键作为组合值。
然后检查散列是否存在,如果不存在则用数据生成一个新对象并将其推送到结果集。
稍后将 weight
添加到散列对象的 属性。
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; }
您可以通过 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%; }
你可以这样做。它可能没有那么有效,但它确实有效。
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);