用Lodash或下划线将对象按多列分组。

问题描述 投票:9回答:5

我有以下对象 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 分割不能正确进行。

javascript underscore.js lodash
5个回答
9
投票

使用下划线的解决方案。

    var props = ['userId', 'replyToId'];

    var notNull = _.negate(_.isNull);

    var groups = _.groupBy(record.notes, function(note){
        return _.find(_.pick(note, props), notNull);
    });

1
投票

这也许可以做得更漂亮,但应该可以用。

lodash.mixin({
  splitGroupBy: function(list, groupByIter) {
    var _ = this, groupBy;
    if (lodash.isArray(groupByIter)) {
      groupBy = function(obj) {
        return _(obj) .pick(groupByIter)
                      .values()
                      .without(null, undefined)
                      .first();
      };
    } else {
      groupBy = groupByIter;
    }
    var groups = _.groupBy(list, groupBy);
    return groups;
  }
});

1
投票

你可以把你的属性列表映射到它们各自的值上 然后选取第一个非伪造的值作为你的组键:

_.mixin({
    splitGroupBy: function(list, groupByIter){
        if (!_.isArray(groupByIter))
            return _.groupBy(list, groupByIter);

        return _.groupBy(list, function(o) {
            var values = _.map(groupByIter, function(k) {
                return o[k];
            });
            return _.find(values);
        });
    }
});

var data = {  
   "notes":[  
      {  
         "id":1,
         "userId":2,
         "replyToId":null
      },
      {  
         "id":5,
         "userId":3,
         "replyToId":null
      },
      {  
         "id":2,
         "userId":null,
         "replyToId":2
      }
   ]
};

_.mixin({
    splitGroupBy: function(list, groupByIter){
        if (!_.isArray(groupByIter))
            return _.groupBy(list, groupByIter);

        return _.groupBy(list, function(o) {
            var values = _.map(groupByIter, function(k) {
                return o[k];
            });
            return _.find(values);
        });
    }
});

snippet.log(JSON.stringify(_.splitGroupBy(data.notes,['userId', 'replyToId'])));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

1
投票

假设... userIdreplyToId 是相互排斥的(即你要么有一个 userIdreplyToId但绝不是两者都有),因为它们在样本数据中,然后指定一个自定义分组函数就可以了。

_.groupBy(data.notes, function(note) {
    return note.userId || note.replyToId;
});

1
投票

你可以使用stringify对象作为键。

_.groupBy(notes, ({ userId, replyToId }) => JSON.stringify({ userId, replyToId }));

输出。

{
  "{\"userId\":2,\"replyToId\":null}": [
    {
      "id": 1,
      "description": "hey",
      "userId": 2,
      "replyToId": null,
      "postId": 2,
      "parentId": null
    }
  ],
  "{\"userId\":3,\"replyToId\":null}": [
    {
      "id": 5,
      "description": "hey test",
      "userId": 3,
      "replyToId": null,
      "postId": 2,
      "parentId": null
    }
  ],
  "{\"userId\":null,\"replyToId\":2}": [
    {
      "id": 2,
      "description": "how are you",
      "userId": null,
      "replyToId": 2,
      "postId": 2,
      "parentId": null,
      "user": null
    }
  ]
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.