3

我有一个 JSON 数据,我需要做一些类似 group by 的事情,我在这里之前问过这个问题,但我没有得到任何满意的答案,所以这次我想更深入地解释一下。

首先,谁能解释一下 javascript 和 sql 之间的区别,groupby因为orderby在 sql 中我们需要聚合函数才能使用group by. 但我不想要聚合函数之类的东西。在这里,我提供了一个非常示例的 JSON 数据和我正在寻找的输出。

所有作者姓名应按sortby字母数字顺序排列。

JSON数据:

var myObject = {
    "Apps": [
        {
            "Name": "app1",
            "id": "1",
            "groups": [
                {
                    "id": "1",
                    "name": "test group 1",
                    "category": "clinical note",
                    "author": "RRP"
                }, {
                    "id": "2",
                    "name": "test group 2",
                    "category": "clinical image",
                    "author": "LKP"
                }, {
                    "id": "3",
                    "name": "test group 3",
                    "category": "clinical document",
                    "author": "RRP"
                }, {
                    "id": "4",
                    "name": "test group 4",
                    "category": "clinical note",
                    "author": "John"
                }
            ]
        }
    ]
}

预期输出:

John
  4 testgroup4 clinicalnote 
RRP
  1 testgroup1  clinicalnote
  3 testgroup3  clinicaldocument
LKP
  2 testgroup2 clinicalimage    

任何想法/建议/方向/想法都会有很大帮助。

4

3 回答 3

3

你可以使用Underscore.js轻松做到这一点:

_.chain(myObject.Apps[0].groups).sortBy("author").groupBy("author").value();

输出一个 JSON 对象:

{
 "John":[{"id":"4","name":"test group 4","category":"clinical note","author":"John"}],
 "LKP":[{"id":"2","name":"test group 2","category":"clinical image","author":"LKP"}],
 "RRP":[{"id":"1","name":"test group 1","category":"clinical note","author":"RRP"},{"id":"3","name":"test group 3","category":"clinical document","author":"RRP"}]
}
于 2014-02-09T07:55:05.777 回答
1

对于这种情况,Javascript 中没有内置的“group by”或“order by”。您将不得不手动执行此操作。这样的事情可能会有所帮助:

var groups = myObject.Apps[0].groups;
var authors = {};
var authorNames = [];

for(var i = 0; i < groups.length; i++) {
    var group = groups[i];    

    if(typeof authors[group.author] === "undefined") {
        authors[group.author] = [];
        authorNames.push(group.author);
        authorNames.sort();
    }

    authors[group.author].push({
        id: group.id,
        name: group.name,
        category: group.category
    });       
}

通常在关联数组中,您并不真正关心键的顺序,并且在迭代顺序时通常不能保证。我在这里所做的是按排序顺序维护一个单独的名称数组。然后我可以遍历该数组并使用这些值从关联数组中获取关联对象。

看看小提琴

于 2012-06-18T21:01:14.077 回答
0

使用一流的函数通过键化简器和排序器创建通用组。我将从我以前的答案中提取减速器。这样,您就可以按您喜欢的任何键进行排序和分组,就像您在 SQL 中所做的那样。

const groupBy = key => (result,current) => {
  const item = Object.assign({},current);
  if (typeof result[current[key]] == 'undefined'){
    result[current[key]] = [item];
  }else{
    result[current[key]].push(item);
  }
  return result;
};

const stringSortBy = key => (a,b) => {
   const sa = a[key];
   const sb = b[key];
   return sa < sb ? -1 : +(sa > sb);
};

const myObject = {
    "Apps": [
        {
            "Name": "app1",
            "id": "1",
            "groups": [
                {
                    "id": "1",
                    "name": "test group 1",
                    "category": "clinical note",
                    "author": "RRP"
                }, {
                    "id": "2",
                    "name": "test group 2",
                    "category": "clinical image",
                    "author": "LKP"
                }, {
                    "id": "3",
                    "name": "test group 3",
                    "category": "clinical document",
                    "author": "RRP"
                }, {
                    "id": "4",
                    "name": "test group 4",
                    "category": "clinical note",
                    "author": "John"
                }
            ]
        }
    ]
}

const categories = myObject.Apps[0].groups.reduce(groupBy('category'),{});
console.log(categories);
const sorted = myObject.Apps[0].groups.sort(stringSortBy('author'));
console.log(sorted);

于 2019-01-11T13:34:04.370 回答