[英]UnderscoreJS Group list of objects
I have an array of objects from a database that I want to collapse for succinctness. 我有一个数据库中的对象数组,为了简洁起见,我希望对其进行折叠。 I've imported underscore.js to help the process, and am unsuccessfully trying to use the 'groupBy' function provided to condense the following list:
我已经导入了underscore.js来帮助完成此过程,但尝试使用提供的“ groupBy”功能来压缩以下列表未成功:
[
{ID:2570,name:"jim",latitude:59.4,longitude:-7.29},
{ID:2573,name:"joe",latitude:54.4,longitude:-7.36},
{ID:2573,name:"joe",latitude:54.3,longitude:-7.37},
{ID:2574,name:"bob",latitude:58.4,longitude:-7.31},
{ID:2574,name:"bob",latitude:58.6,longitude:-7.38},
{ID:2574,name:"bob",latitude:58.8,longitude:-7.39},
{ID:2575,name:"mary",latitude:54.1,longitude:-7.30},
]
To the format: 格式:
[
{ID:2570, name:"jim", locs : [[59.4,-7.29]]},
{ID:2573, name:"joe", locs : [[54.4,-7.36], [54.3,-7.37]]} //etc...
]
Following your intuition to use _.groupBy
, 遵循使用
_.groupBy
的直觉,
var data = [ {ID:2570,name:"jim",latitude:59.4,longitude:-7.29}, {ID:2573,name:"joe",latitude:54.4,longitude:-7.36}, {ID:2573,name:"joe",latitude:54.3,longitude:-7.37}, {ID:2574,name:"bob",latitude:58.4,longitude:-7.31}, {ID:2574,name:"bob",latitude:58.6,longitude:-7.38}, {ID:2574,name:"bob",latitude:58.8,longitude:-7.39}, {ID:2575,name:"mary",latitude:54.1,longitude:-7.30}, ]; var groups = _.groupBy(data, function(locationObject) { return locationObject.ID; }); var result = _.map(groups, function(group) { var condensed = {ID: group[0].ID, name: group[0].name}; condensed.locs = _.map(group, function(row) { return [row.latitude, row.longitude]; }); return condensed; }); document.getElementById('log').innerHTML = JSON.stringify(result, null, ' ');
<script src="http://underscorejs.org/underscore-min.js"></script> <pre id="log"></pre>
I think that you can use _.reduce
method this way: 我认为您可以通过以下方式使用
_.reduce
方法:
_.reduce(data, function(prev, curr) {
var obj = _.findWhere(prev, {ID: curr.ID});
if (obj) {
obj.locs.push([curr.latitude, curr.longitude]);
}
else {
curr.locs = [[curr.latitude, curr.longitude]];
prev.push(_.omit(curr, ['latitude', 'longitude']));
}
return prev;
}, []);
var
input = [
{ID:2570,name:"jim",latitude:59.4,longitude:-7.29},
{ID:2573,name:"joe",latitude:54.4,longitude:-7.36},
{ID:2573,name:"joe",latitude:54.3,longitude:-7.37},
{ID:2574,name:"bob",latitude:58.4,longitude:-7.31},
{ID:2574,name:"bob",latitude:58.6,longitude:-7.38},
{ID:2574,name:"bob",latitude:58.8,longitude:-7.39},
{ID:2575,name:"mary",latitude:54.1,longitude:-7.30},
]
, output = {} //Handles filtering
, outputArray = [] //Not mandatory, if you want output to be an array
;
for(var key,i=-1;++i<input.length;){
key = input[i].ID; //if ID is unique and ID/name combination is always same
//key = input[i].ID + input[i].name; //if not
if(!output[key]){
output[key] = {ID : input[i].ID, name : input[i].name, locs : []};
outputArray.push(output[key]);
}
output[key].locs.push([input[i].latitude, input[i].longitude]);
}
console.dir(output);
console.dir(outputArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.