我有以下对象records
:
{
"notes":[
{
"id":1,
"description":"hey",
"userId":2,
"replyToId":null,
"postId":2,
"parentId":null
},
{
"id":5,
"description":"hey test",
"userId":3,
"replyToId":null,
"postId":2,
"parentId":null
},
{
"id":2,
"description":"how are you",
"userId":null,
"replyToId":2,
"postId":2,
"parentId":null,
"user":null
}
]
}
我想将其输出为:
2
object with id 1
object with id 2 (because replyToId value is same as userId
3
object with id 5
所以基本上我想考虑同一组下的UserId和replyToId值。
我在 lodash 下构建了自己的 mixin,将 groupBy 方法包装为:
mixin({
splitGroupBy: function(list, groupByIter){
if (_.isArray(groupByIter)) {
function groupBy(obj) {
return _.forEach(groupByIter, function (key){
if ( !!obj[key] ) return obj[key]
});
}
} else {
var groupBy = groupByIter;
}
debugger;
var groups = _.groupBy(list, groupBy);
return groups;
}
});
调用看起来像这样:
_.splitGroupBy(data.notes,['userId', 'replyToId']);
输出没有分组。即使我尝试过_.map
反而_.forEach
分裂没有正确发生。